Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

view.frame.width IS correct in viewDidLoad with storyboard, why?

Tags:

ios

Ok, so this is somewhat of a weird question. I've been developing iOS-apps for quite a while and have always respected that viewDidLoad is the incorrect place for using the frame of the view.

However, in one app that I'm about to solve some bugs for, it seems that the frame is actually set correctly in viewDidLoad!

The storyboard is using "View as: iPhone 7", so I'm expecting to get 375px width for the frame in viewDidLoad. I'm using normal push segues.

However, the width is actually set correctly to the current devices screen width. I'm using a simulator.

What am I missing? Can we use the width for calculations in viewDidLoad nowadays?

like image 829
ullstrm Avatar asked Jun 12 '17 13:06

ullstrm


People also ask

How do I change the width of a view in Swift?

You can do this in Interface Builder: 1) Control-drag from a frame view (e.g. questionFrame) to main View, in the pop-up select "Equal heights". 2)Then go to size inspector of the frame, click edit "Equal height to Superview" constraint, set the multiplier to 0.7 and hit return.

What is a views frame?

A view's frame specifies the view's size and its position relative to its superview. Because a view's size is always specified by its frame, a view is always a rectangle. A CGRect contains the members origin and size.

What is the difference between frames and bounds?

Frame is used by the view's parent view to place it inside the parent view. Bounds is used by the view itself to place it's own content (like a scroll view does while scrolling).

Is viewDidLoad only called once?

viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.


1 Answers

No you still can't use viewDidLoad for anything size/position related.

The first method that will have the actual "proper" dimensions is the viewDidLayoutSubviews.

Regarding the phenomenon you are experiencing.

You are probably talking about the "root view" from your view controller.

As the apple docs say (bolding is mine):

A view controller’s root view is always sized to fit its assigned space. For other views in your view hierarchy, use Interface Builder to specify the Auto Layout constraints that govern how each view is positioned and sized within its superview’s bounds. You can also create constraints programmatically and add them to your views at appropriate times. For more information about how to create constraints, see Auto Layout Guide.

https://developer.apple.com/documentation/uikit/uiviewcontroller

This is why you are getting the correct width.


If you'd like to test, try adding a subview that fills the whole "root view" area. This one will probably just give you the dimensions that that are shown on the Interface Builder.

My test results:

Interface Builder -> "View as iPhone 7"

Simulator iPhone 5s:

Root View dimensions: (0.0, 0.0, 320.0, 568.0)
Sub View dimensions: (0.0, 0.0, 375.0, 667.0)

Simulator iPhone 7:

Root View dimensions: (0.0, 0.0, 375.0, 667.0)
Sub View dimensions: (0.0, 0.0, 375.0, 667.0)

Simulator iPhone 7 Plus:

Root View dimensions: (0.0, 0.0, 414.0, 736.0)
Sub View dimensions: (0.0, 0.0, 375.0, 667.0)

Interface Builder -> "View as iPhone 4s"

Simulator iPhone 5s:

Root View dimensions: (0.0, 0.0, 320.0, 568.0)
Sub View dimensions: (0.0, 0.0, 320.0, 480.0)

Simulator iPhone 7:

Root View dimensions: (0.0, 0.0, 375.0, 667.0)
Sub View dimensions: (0.0, 0.0, 320.0, 480.0)

Simulator iPhone 7 Plus:

Root View dimensions: (0.0, 0.0, 414.0, 736.0)
Sub View dimensions: (0.0, 0.0, 320.0, 480.0)

As you can see, the root view dimensions will always correspond to the device you are using as stated in the documents. While any other views will have the dimensions you can see on the interface builder.


This question will also help you understand how the life cycle works: Why am I having to manually set my view's frame in viewDidLoad?

like image 177
Pochi Avatar answered Oct 11 '22 21:10

Pochi