[英]Property observer working for bounds but not frame
我正在做這個項目,並且在故事板上有一個場景。
場景是一個TableViewController。
表格視圖有一個自定義的原型單元(鏈接到CustomCell.swift)。
現在,我希望在自定義視圖上繪制的內容在視圖大小更改時發生變化,以便在設備旋轉時將其調整為新的單元格寬度。 由於這些限制,在旋轉設備后,當CustomCell更改大小時,CustomView的框架將更改。 為了檢測到這一點,我向CustomView.swift添加了兩個屬性觀察器:
override var frame: CGRect {
didSet {
print("Frame was set!")
updateDrawing()
}
}
override var bounds: CGRect {
didSet {
print("Bounds were set!")
updateDrawing()
}
}
運行項目時,第二個觀察者在旋轉設備時工作正常。 第一個觀察者沒有。 我的問題是,為什么第一個觀察者沒有檢測到鏡架已改變?
.frame是根據視圖(和變換)的.bounds和.center計算的,因此它不會改變。 為了對旋轉做出反應,請覆蓋此設置(從iOS8開始):
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (coordinator) -> Void in
// do your stuff here
// here the frame has the new size
}, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.