Search code examples
iosswiftautolayoutframesbounds

Frames vs Bounds and autolayout constraints?


I recently read a wonderful question regarding frames & bounds property of views over here - UIView frame, bounds and center. I tried to experiment on that a bit more with autolayout as well and now I find myself in a very conflicting scenario. Please be patient as it may take a bit long.

I will highlight them one by one & so please help me to get over all these -

What I have? -

A view hierachy with constraints as follows - (The designing was done on the default ViewController size of 600*600)

enter image description here

enter image description here

I tried to print out the frames and bounds of these views -

contentView

 print(contentView.frame)// o/p - (0.0, 64.0, 600.0, 536.0) 
  print(contentView.bounds)//o/p- (0.0, 0.0, 600.0, 536.0)

Q) **a)**Why is the frame size showing 600*536 whichever screen I may run it. eg. It prints the same if I run it on a 4s screen or else in an iPad screen?

**b)**And it always infers the size of my VC in my storyboard!!. eg. If i design the screen in a 3.5 inch VC,the frame size becomes 320.0*416.0.

**c)**Since it is pinned to all sides of the view it should be same to the view size, and also it always remains pinned to edges. So why is it not of the same size of self.view.frame.width*self.view.frame.height

sideView

print(sideView.frame)//o/p - (520.0, 86.0, 80.0, 128.0) 
   print(sideView.bounds)//o/p - (0,0, 80.0, 128.0)

Q) If I design a custom UIView programmatically as

let TestView = UIView(frame: CGRectMake(520.0, 86.0, 80.0, 128.0))//Mark:its frame is exactly same as sideView.frame
        TestView.backgroundColor = UIColor.redColor()
        self.contentView.addSubview(TestView)

So here the sideView(subview of contentView) should be replaced by the TestView but isn't & the o/p screen comes the same as in previous case. Why?

Thanks.


Solution

  • Q) **a)**Why is the frame size showing 600*536 whichever screen I may run it. eg. It prints the same if I run it on a 4s screen or else in an iPad screen?

    **b)**And it always infers the size of my VC in my storyboard!!. eg. If i design the screen in a 3.5 inch VC,the frame size becomes 320.0*416.0.

    It will depend on when you check the size of the content view (or the view controller's main view). If you were to check the size in the viewDidLoad() method, you'd see it's the size you see in Interface Builder. That's the size it was saved in the storyboard, and it hasn't been resized for the device yet.

    However, if you check the size in the viewDidAppear() method, it should be a size that fits on the device's screen.

    If you actually want to mess with the frames directly, you'd probably want to do that in viewDidLayoutSubviews(). At this point the view will have been resized for the device, and you can get the updated measurements and modify the frames. Although it's usually best to just set up constraints properly instead, and let auto layout make the changes.