簡體   English   中英

自定義 UICollectionViewCell 中的圓角在 Swift 中不起作用

[英]Round Corners in custom UICollectionViewCell not working in Swift

我有自定義 UICollectionViewCell 並嘗試為按鈕圓角,這不起作用。 我對 ViewController 有同樣的問題,問題是我在 viewDidLoad 而不是 subviewsDidLoad 中進行舍入。

我不知道現在有什么問題。

分享我的代碼。

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        initialsButton.layer.cornerRadius = 0.5 * initialsButton.frame.size.width
        initialsButton.clipsToBounds = true
        initialsButton.layer.masksToBounds = true
    }

-> 但我也試過沒有.clipsToBounds 和.masksToBounds。 結果相同。

這是結果。 這不是一個圓圈,它使角落錯誤地看到這個結果

我想你正在使用 IBOutlet 的 Create a class 類似的東西,在 storyboard 中的單元格中添加按鈕,在那里編輯你想要的任何東西,它更容易。 您的代碼不好,只需刪除大小,如果您要這樣做, frame.height / 2 就是您想要的。

@IBDesignable
class RoundedBtn: UIButton {

@IBInspectable var cornerRadius: Double {
    get {
        return Double(self.layer.cornerRadius)
    }set {
        self.layer.cornerRadius = CGFloat(newValue)
    }
}

@IBInspectable var borderWidth: Double {
    get {
        return Double(self.layer.borderWidth)
    }
    set {
        self.layer.borderWidth = CGFloat(newValue)
    }
}

@IBInspectable var borderColor: UIColor? {
    get {
        return UIColor(cgColor: self.layer.borderColor!)
    }
    set {
        self.layer.borderColor = newValue?.cgColor
    }
}

@IBInspectable var shadowColor: UIColor? {
    get {
        return UIColor(cgColor: self.layer.shadowColor!)
    }
    set {
        self.layer.shadowColor = newValue?.cgColor
    }
}

@IBInspectable var shadowOffSet: CGSize {
    get {
        return self.layer.shadowOffset
    }
    set {
        self.layer.shadowOffset = newValue
    }
}

@IBInspectable var shadowRadius: Double {
    get {
        return Double(self.layer.shadowRadius)
    }set {
        self.layer.shadowRadius = CGFloat(newValue)
    }
}

@IBInspectable var shadowOpacity: Float {
    get {
        return self.layer.shadowOpacity
    }
    set {
        self.layer.shadowOpacity = newValue
    }
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

func commonInit() {
    self.titleLabel?.numberOfLines = 0
    self.titleLabel?.textAlignment = .center
    self.setContentHuggingPriority(UILayoutPriority.defaultLow + 1, for: .vertical)
    self.setContentHuggingPriority(UILayoutPriority.defaultLow + 1, for: .horizontal)
}

override var intrinsicContentSize: CGSize {
    let size = self.titleLabel!.intrinsicContentSize
    return CGSize(width: size.width + contentEdgeInsets.left + contentEdgeInsets.right, height: size.height + contentEdgeInsets.top + contentEdgeInsets.bottom)
}

override func layoutSubviews() {
    super.layoutSubviews()
    titleLabel?.preferredMaxLayoutWidth = self.titleLabel!.frame.size.width
}

}

問題是我在 awakeFromNib() 中做圓角,而不是你必須在 layoutSubviews() 中做它,它工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM