Make TableView cell shadow

在layer下有四個參數可以設定陰影

  1. shadowColor(陰影的顏色)
  2. shadowRadius(陰影的圓角半徑)
  3. shadowOpacity(陰影的透明程度)
  4. shadowOffset(陰影的發散程度)

但當你在使用時,常會被以下問題阻礙

view的shadow會被masksTobounds/clipsToBonuds裁掉

  • view.layer.masksToBounds
他會依layer的bounds將一塊mask蓋上layer裁切,包含任何bounds外的效果,若不只一個mask存在,則mask的效果是會疊加
  • view.clipsToBounds
直白的就是將bounds外的子畫面才切掉

解法

建立兩個View,一個負責shadow,另一個負責border

  1. borderView的masksToBounds設為true(這樣才不會讓borderView的預設效果蓋住shadowView)
  2. shadowView的masksToBounds設為false

建立一個UIView的subclass,對其override bounds的限制,將陰影效果設於此處

  1. 將該view subclass ShadowView
  2. masksToBounds記得設為false(不然效果都會被蓋掉)

--

--

On my way to become an iOS developer!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store