Recently i am writing an photo extension for my app.
However, i find the extension's storyboard cannot figure out the frame for each view with AutoLayout correctly.
For example I set this in the storyboard:
However when i run the simulator it shows:
Also, when i check the frame for my customized view in didMoveToWindow()
, it also shows the incorrect frame :
(66.0, 512.0, 468.0, 30.0)
Actually, the customized slider works fine in my app's storyboard.
What's wrong with me?
Ps: the slider here is a customized UIView
not a UISlider
Edit:
By replacing the class to UIView (with color) on storyboard, i am now sure it is not the problem of Autolayout constraints because everything works fine.
Then i found the problem is in the process that I init this customized UIView, I set up my knobs and bar in the method didMoveToWindow()
, whose frames depend on the View's frame. When i print the frame of the View at the top of didMoveToWindow()
, the frame is not correct at all.
So I cannot get the correct frame of a UIView in its didMoveToWindow()
method when using Autolayout?
But that works fine in my original app. So i am confused.
Edit2
I use a delay function to re-check its frame after 5 seconds :
func delay(#seconds: Double, completion:()->()) {
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * seconds ))
dispatch_after(popTime, dispatch_get_main_queue()) {
completion()
}
}
override func didMoveToWindow() {
delay(seconds: 5, {
println(self.frame)
})
}
5 seconds later, the frame is correct! So, when the method is called, constraints are not applied yet. So now what i need to do is to get call-back in UIView when its superView's ViewController finish ViewDidLoad
.
Edit3
I find the answer. I should set up subView's frame in UIView's layoutSubviews()
method. In that case i can get the right frame when using Autolayout.
Actually, after the UIView's constraints are applied, it will call its layoutSubviews()