0

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:

enter image description here

enter image description here

However when i run the simulator it shows: enter image description here

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()

duan
  • 8,515
  • 3
  • 48
  • 70

2 Answers2

1

Read this => it's helps you alot :)

learn-to-love-auto-layout OR if you want to LOVE AUTO LAYOUT… PROGRAMMATICALLY 1 i/p image 2 i/p image out put image

Yuyutsu
  • 2,509
  • 22
  • 38
0

Rather than giving trailing and leading space, apply this simple constraints.

  1. Make your height constant (If you want to!)
  2. Put UIView in center of the container
  3. Give "Horizontal Center in Container" constraint
  4. Apply appropriate "top space" and "bottom space"

That's it! You're done.

Pang
  • 9,564
  • 146
  • 81
  • 122
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57