Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically resize a XIB's root view to the size of its contents with Autolayout

I create a XIB in Xcode and add a simple view as a subview:

XIB with rootview and subview

What I want to achieve is that the subview has a fixed size and the rootview automatically resizes to the size of that subview, leaving a margin of 20.0 around it:

Resized XIB

So I add a fixed width and a fixed height constraint to the subview. Then I add the four constraints for the 20.0 margin:

Constraints View showing constraints

As the superview does not have any constraints there should be neither ambiguity nor conflicting constraints: I would expect the superview to shrink down in order to match the constraints. However, Xcode complains:

Warnings

These constraints would only be conflicting if the rootview had a fixed size and that appears to be the case. So my question is: How can I make the rootview of a XIB flexible so that it dynamically adjusts its size to match its contents?

(Is that even possible with Interface Builder?)

like image 511
Mischa Avatar asked Sep 29 '15 09:09

Mischa


1 Answers

How can I make the rootview of a XIB flexible so that it dynamically adjusts its size to match its contents?

Not possible Interface builder.

As the superview does not have any constraints there should be neither ambiguity nor conflicting constraints

Its not just a super view. Its also an objet in nib. We define simulated size for such views. This could be used to silence the errors. But again these are just simulated.

Is this rootView a view controllers view ? If yes i don't understand why are you trying to fix its withd to 280 and height to 168.

If this view is a custom view that you are going to add to another 'parent' view. Then you should change you simulated size to with 280 and height 168, and when adding this as subview you need to add two more constraints to position this rootview in the 'parent' view.

like image 113
BangOperator Avatar answered Oct 15 '22 21:10

BangOperator