Xcode Interface Builder Height / Width and View Controller Settings

I'm having a hard time wrap my head around how the frame properties (height, width, posX, posY) work in terms of setting them in the View Controller and the Storyboard (Interface Builder).

For example, let's say I have an object UICollectionView

that I set for a width of 400 and a height of 800. Then in my code, I set the frame of the same object to 600 x 400. I didn't actually find consistent behavior. I tried to set the frame to viewDidLayoutSubviews

and it kind of worked - but it seemed to "jump" back and forth between what was set on the storyboard.

Basically my question is when do properties on a storyboard change the UI object? I guess I just need to know this and then reset them in the View Controller after the fact. Or, is there a way to set the height and width to empty so I can do it all in code?

Any insight on this would be greatly appreciated!

+3


source to share


2 answers


If you are using AutoLayout

in your project, setting the frames of the view objects that you set up in the storyboard will not work. Because after you set the frames, AutoLayout will update the frames again, which will make the frames you have set not work. If you need more details, you can check this article: Advanced Auto Layout Tools But you can set the frames of programmatically created view objects to place them.

You can check if the storyboard file is included in AutoLayout file inspector

. However, there is one thing, if you want to use AutoLayout, don't set the view translatesAutoresizingMaskIntoConstraints

to NO

. The default value for this property is YES

. If you are using AutoLayout, this property is set by the storyboard for you because the constraints created in the storyboard are enough to expand the views.

EDIT:



1) if you are not using AutoLayout then setting the frames in the code should work as expected.

2) yes you can, a little more complicated. you have to build it UICollectionView

yourself using [[UICollectionView alloc] init]

or load it from nib. and then configure the cell in IB with the xib file . you can use AutoLayout to hang cell layouts in xib file . and register the cell class in UICollectionView

or load the cell object from nib yourself. then you have to calculate the size of each cell and let the AutoLayout swap to place the cells.

while it's simpler than the layout interface entirely in code, it's still a bit more complex. the best way is to use AutoLayout. As not all details of the layout can be done at design time, as the frame of some views may differ depending on the data. you can first create the layout basic

with AutoLayout and then the IBOutlet

constraints you want to adjust on the fly. and change the propertyconstant

restriction objects later. this way, you can have 100% control over the layout process, and also let AutoLayout do dirty jobs that you don't want to do yourself. I suggest you read the official AutoLayout docs and other helpful resources. The learning curve is steep at first, it can make you kill yourself too. But it is really powerful and easy to use. once you figure out how AutoLayout works, it will make your iOS development life much easier.

+3


source


If you want to set the code size through code, you can try creating outputs from storyboard to view controller. Then in the view controller you can use viewDidLoad

or viewDidAppear

to set the size properties of your object (s). viewDidLoad

will be called when this view is first created. viewDidAppear

will be called every time the view returns to the screen (for example if you return with a navigation controller).



0


source







All Articles