13

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?)

Mischa
  • 15,816
  • 8
  • 59
  • 117
  • Anyone, who has this problem, please don't worry. To suppress XIB errors just set the proper size of the view. Then, when you will create the view, it will be resized to meet constraints automatically. – kelin Feb 06 '18 at 21:53

4 Answers4

2

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.

BangOperator
  • 4,377
  • 2
  • 24
  • 38
  • The thing is: The root view is supposed to be a table view cell and if I want to use it with autolayout I need the root view to first adapt to the constraints so I can then get the frame's height to return it from the `tableView:heightForRowAtIndexPath:` method. – Mischa Oct 02 '15 at 20:50
  • If you are trying to get cell height via this. You are in a wrong direction. – BangOperator Oct 03 '15 at 03:55
  • So what would be the right direction in your opinion? – Mischa Oct 03 '15 at 08:39
  • Well, I have actually used this concept myself and in my opinion it's pretty much the same direction as I was going here: Trying to let the cell (auto) layout itself first, then retrieving its height and returning it in the `tableView:heightForRowAtIndexPath:` method. The only difference is that I was trying to let the XIB's *root view* resize itself whereas the other post suggests to only let's the cell's *contentView* resize itself. I still believe it's a valid question whether this is possible for views other than UITableViewCells as well because it would make auto layout a *lot* easier. – Mischa Oct 03 '15 at 09:09
  • 1
    Yes, its definitely a valid question! Cheers for trying other ways. – BangOperator Oct 03 '15 at 10:01
  • @BangOperator: can you please elaborate a little on these two extra constraints for custom views that go into parent views? – Janus Varmarken Sep 11 '16 at 11:36
  • @JanusVarmarken these two constraints would be for centre align the view vertically and horizontally. Just these two constraints would be enough as the height and with will be calculated from the child views height width plus the offsets. – BangOperator Sep 11 '16 at 18:41
  • See answer below – Garnik Aug 24 '19 at 12:54
-1

I had a same issue. I have a view in xib, which had dynamic content and it needed to fit into other superviews. so here is the answer how I achieved that

First you need to

myViewFromXib.translatesAutoresizingMaskIntoConstraints = false to prevent translating autoresizing mask into constraints. Then add subview superViewForMyViewFromXib.addSubview(myViewFromXib). And just add your constraints 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()
Garnik
  • 423
  • 1
  • 6
  • 20
  • "I would expect the superview to shrink down in order to match the constraints". OP wants the superview to shrink right there in the interface builder, just when he applies the constraints, not at runtime. – BangOperator Aug 25 '19 at 18:30
-2

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

  1. add a constraint to a subview for the height
  2. open the xib as a text file
  3. find the constraint you added (eg by Cmd-F'ing to the value of the height)
  4. cut and paste it into the root view's constraint section
  5. Open the xib in interface builder again
  6. The constraint appears and you can edit it like normal.
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
-5

OMG, I cant believe you accept that this is not possible and change your way , if this was not possible then Xib would be useless. please don't provide wrong info to others your question is well detail but answer is more than poor:

answer is more than easy :

        subview.frame.size.height = rootView.frame.size.height
        subview.frame.size.width = rootView.frame.size.width
PPPPPPPPP
  • 660
  • 7
  • 14