【发布时间】:2014-12-02 11:43:36
【问题描述】:
我正在尝试在其边界发生变化时更新我的视图。我的视图在其父视图中使用 Auto Layout 进行布局。
这是我观察边界的代码:
public override init() {
super.init()
self.addObserver(self, forKeyPath: "bounds", options: .New, context: nil)
}
public override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
NSLog("[SkewedSplitView]: observer: %fx%f, %f:%f", self.bounds.width, self.bounds.height, self.bounds.origin.x, self.bounds.origin.y)
}
输出:
2014-12-02 12:35:35.018 XXX[7365:2001261] [SkewedSplitView]:观察者: 0.000000x0.000000, 0.000000:0.000000
我什至尝试过瑞士观察员:
override public var bounds: CGRect {
didSet {
NSLog("[SkewedSplitView]: bounds: %fx%f, %f:%f", self.frame.width, self.frame.height, self.frame.origin.x, self.frame.origin.y)
}
}
输出:
2014-12-02 12:35:35.012 XXX[7365:2001261] [SkewedSplitView]:边界: 0.000000x0.000000, 0.000000:0.000000
两者都只调用了一次。 这是我布置视图的代码(使用制图):
let betters = SkewedSplitView()
betters.backgroundColor = UIColor.greenColor()
self.addSubview(betters)
constrain(betters, friends) { (betters, friends) in
betters.bottom == friends.top - 3
betters.leading == betters^.leading
betters.trailing == betters^.trailing
betters.height == 40
}
在屏幕上,视图布置得很好,有正确的框架。
【问题讨论】:
标签: ios swift autolayout key-value-observing