我有一個需要圓形的 UIView 子類。我遇到的問題是,這個視圖是用框架實體化的(并最終調整大小),在呼叫初始化程式時.zero會產生0 的值。cornerRadiusmakeRound
我應該呼叫哪個 UIView 生命周期方法makeRound并假設layer.bounds已采用其最終值(非零)?
fileprivate extension UIView {
func makeRound() {
layer.cornerRadius = layer.bounds.width*0.5
clipsToBounds = true
}
}
我可以使用的唯一 UIView 子類初始化程式是
public init() {
super.init(frame: .zero)
// init routines
}
uj5u.com熱心網友回復:
我要么覆寫bounds屬性,要么覆寫layoutSubviews方法。
class SomeView: UIView {
// Either do this:
override var bounds: CGRect {
didSet {
layer.cornerRadius = bounds.size.height / 2.0
// or call makeRound()
}
}
// Or do this:
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.height / 2.0
// or call makeRound()
}
}
不要兩者都做。選擇兩者之一。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526046.html
標籤:迅速uikit
