Dinesh Nagarajan
Dinesh Nagarajan

Reputation: 1243

Is it possible to set constraints for root view in XIB?

Recently i was following a tutorial to create frameworks in Swift, Where they create reusable visual effect view just like we see in Xcode build success pop up. I was doing the UI step by step where they created a new xib file and dragged and dropped UIVisualEffectView into it.

Empty UIVisualEffectView without constraints

In the above image we can see that the UIVisualEffectView doesn't contain any constraints. It only has auto resizing masks in size inspector(as expected).


But the following image from the tutorial suddenly contains all necessary constraints for it's root view.

UIVisualEffectView with constraints

I'm already aware that we CANNOT, by default set any constraints for the root view. But in the tutorial the author seems to have added constraints for the same. I'm just curious to know if it is possible to do that or am i missing any fundamental idea ?

EDIT:

Constraints for root view.

UIAfterEffectView constraints

Upvotes: 5

Views: 1983

Answers (3)

DonMag
DonMag

Reputation: 77567

OK, found one way to do this...

Haven't done any testing, so I'm not sure if the constraints are really set correctly or will be used by auto-layout at run-time, but this will put them there.

Create a new "View" XIB - this is how it looks to start:

enter image description here

Add a Visual Effect view to this view, and give it width and height constraints of 230 / 230:

enter image description here

Now, drag that Visual Effects view *out of the plain view, onto an empty space:

enter image description here

You will see it maintains its constraints. Then delete the original view, and add the other elements:

enter image description here

Upvotes: 5

matt
matt

Reputation: 535526

I don't know what's going on your tutorial (and I have no intention of watching it), and I have no idea under what circumstances the screen shot was achieved. But I do know the rules for how a view controller's main view gets sized.

Any view controller's main view must expect to be resized as it is placed into the interface. The size in the storyboard is therefore meaningless. That is why you cannot give it a size with auto layout.

If a view in a xib or storyboard is to be used as the main view of a view controller, it is meaningless (and would be wrong) to set constraints on it, because it will be the task of the view controller's parent (or, in the case of the window's rootViewController, of the window) to set the size of the view when it places it into the hierarchy.

If a view controller wants to vote on the size of its view when it is placed into the view hierarchy (for example, as a popover), that is what the content size (preferred explicit size) is for. But even then, the parent is the one who controls what actually happens.

Upvotes: 3

Lukas Würzburger
Lukas Würzburger

Reputation: 6693

As you can actually see on your screenshot the width/height constraints belong to the View (first subview of Visual Effect View).

Look at the green line:

enter image description here

Upvotes: 2

Related Questions