2017-06-26 38 views
0

我是iOS開發新手,嘗試創建一個自定義UITableViewCell,該背景左上角和左下角我想四捨五入,但只適用於頂級菜單,左角。UITableViewCell TopLeft和BottomLeft拐角不起作用

這裏是我的代碼(SWIFT 3):

override func awakeFromNib() { 
    super.awakeFromNib() 

    let bgViewMask = CAShapeLayer() 

    let bgViewPath = UIBezierPath(
     roundedRect: bgView.bounds, 
     byRoundingCorners: [.topLeft, .bottomLeft], 
     cornerRadii: CGSize(width: 8, height: 8) 
    ) 

    bgViewMask.frame = bgView.bounds 
    bgViewMask.path = bgViewPath.cgPath 

    bgView.layer.mask = bgViewMask 
} 

我也試過這layoutSubviews()

override func layoutSubviews() { 
    super.layoutSubviews() 

    //code here 
} 

,但它並沒有正常工作。

如果我使用bgView.layer.cornerRadius = 8它適用於所有角落。

我做錯了什麼?

回答

3

你可以試試這個:

import UIKit 
class CustomImageView: UIImageView { 

override public func layoutSubviews() { 
    super.layoutSubviews() 
    self.roundUpCorners([.topLeft, .bottomLeft], radius: 8) 
} 
} 
extension UIView { 
func roundUpCorners(_ corners: UIRectCorner, radius: CGFloat) { 
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
    let mask = CAShapeLayer() 
    mask.path = path.cgPath 
    self.layer.mask = mask 
} 
} 
+0

它的工作原理,謝謝! –

+0

不客氣。快樂的編碼。 –