Dynamically resize XIB root view to size of its contents using Autolayout

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

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 subtitle, leaving around 20.0%:

Resized XIB

This way I add a fixed width and a fixed height limit in a subview. Then I add four constraints for field 20.0:

Limitations View showing restrictions

Since the supervisor does not have any restrictions, there should be no ambiguity or conflicting restrictions: I would expect the supervisor to shrink to fit the restrictions. However, Xcode complains:

Warnings

These restrictions would be inconsistent if rootview had a fixed size and it seemed to be so. So my question is: How can I create an XIB flexible root view so that it dynamically adjusts its size according to its contents?

(is this possible with Interface Builder?)

+9
xcode resize autolayout interface-builder constraints
source share
5 answers

How can I make rootview XIB flexible so that it dynamically adjusts its size according to its contents?

Impossible interface builder.

Since the supervisor does not have any restrictions, there should be no ambiguity or conflicting restrictions

This is not just a super-look. Its also an object in nib. We define the simulated size for such representations. This can be used to disable errors. But again, they are simply simulated.

Is this viewViewViewView ViewView? If so, then I don’t understand why you are trying to correct your value to 280 and height to 168.

If this view is a custom view, you are about to add it to another parent view. Then you must change your simulated size to 280 and a height of 168, and by adding this as a preview, you need to add two more constraints in order to position this root view in the parent view.

+2
source share

I think you forgot to set the autoscale mask for the entire container

self.yourViewController!.view.translatesAutoresizingMaskIntoConstraints = false 
0
source share

I had the same problem. I have a view in xib that had dynamic content, and it should fit into other superviews. so here is the answer how i got this

First you need

myViewFromXib.translatesAutoresizingMaskIntoConstraints = false to prevent the auto- myViewFromXib.translatesAutoresizingMaskIntoConstraints = false mask from translating into restrictions. Then add the superViewForMyViewFromXib.addSubview(myViewFromXib) . And just add your limitations to superview like this:

 NSLayoutConstraint.activate([ (myViewFromXib.topAnchor.constraint(equalTo: superViewForMyViewFromXib.topAnchor, constant: 0))!, (myViewFromXib.bottomAnchor.constraint(equalTo: superViewForMyViewFromXib.bottomAnchor, constant: 0))!, (myViewFromXib.leadingAnchor.constraint(equalTo: superViewForMyViewFromXib.leadingAnchor, constant: 0))!, (myViewFromXib.trailingAnchor.constraint(equalTo: superViewForMyViewFromXib.trailingAnchor, constant: 0))! ]) superViewForMyViewFromXib.setNeedsLayout() 
-one
source share

You can do this by manually editing xib, for example, to set a height limit (in my case it was an inequality to set a minimum height):

  • add restriction to subzone for height
  • open xib as a text file
  • find the added constraint (e.g. Cmd-F'ing to a height value)
  • cut and paste it into the restrictions section of the root view
  • Run the xib builder again in the interface
  • A restriction appears, and you can edit it as usual.
-2
source share

Oh my god, I can’t believe that you accept that this is impossible, and change your path if it were impossible, Xib would be useless. please do not provide incorrect information to others, your question is detailed, but the answer is more than bad:

The answer is more than simple:

  subview.frame.size.height = rootView.frame.size.height subview.frame.size.width = rootView.frame.size.width 
-2
source share

All Articles