![](/img/trans.png)
[英]UITextView setContentOffset:CGPointZero in layoutSubviews doesn't work in iOS8
[英]Custom UIButton layoutSubviews() doesn't work unless super.layoutSubviews() is called
碼:
class ViewController: UIViewController{
var button = CustomButton()
override func viewDidLoad(){
super.viewDidLoad()
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
//add constraints and etc.
}
}
class CustomButton: UIButton{
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = self.layer.frame.width * 0.5
self.setTitle("abc", forState: .Normal)
}
}
問題1:為什么我必須調用super.layoutSubviews()
才能使setTitle()
工作? (即cornerRadius
確實設置但不是標題)
問題2:我嘗試將我在layoutSubviews()
中的代碼放在drawRect()
但這並沒有改變cornerRadius
。
UIButton
包含一個UILabel
子視圖以顯示其標題。 如果您不調用super.layoutSubviews()
,則該子標簽無法正確設置。
UIButton是一個UIView,它有自己的layoutSubviews
實現。 您仍然希望確保調用代碼,從而調用super.layoutSubviews()
。 除了默認實現(而不是默認實現)之外,您還需要設置cornerRadius
和title
以便在調用super之后添加該代碼。
如果未覆蓋方法周期,則在調用layoutIfNeeded
時,默認情況下將調用UIView中的layoutSubviews()
。 如果你覆蓋它並且不調用super.layoutSubviews()
那么你基本上只是刪除了Apple的方法實現。
這就是在覆蓋UIViewController子類中的方法時應該調用super.viewDidLoad()
原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.