3

I have a view (let's call it "container"), I want to place some views inside the container and I want to place the container as the left item in a UINavigationBar. So:

UIView* container;
UIBarButtonItem* barButtonItem;

container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
/*
add some views to container, setup auto-layout constraints for
subviews
*/

barButtonItem = [[UIBarButtonItem alloc] init];
[barButtonItem setCustomView:container];
[[navigationBar topItem] setRightBarButtonItem: barButtonItem];

Now, I would like 'container' to fill (stretch or compress) dynamically the avilable space as the navigation bar width changes (due to changing the oriantion, iOS 9 split view etc.)

• If I just use the code as above - the width remains constant 200 (or whatver I set in the initialiser)

• If I set the autoresizing mask to UIViewAutoresizingFlexibleWidth then a very erratic behaviour (which can lead to 'container' to disappear altogether)

• If I set translatesAutoresizingMaskIntoConstraints to NO and then set the autolayout constraints myself to 'container' then the view is resized but it is positioned in the wrong place.

I'm assuming the bad positioning in the last case is due to not specifying constraints for horizontal and vertical positining but I'm not sure how to specify these as I don't know which view will contain 'container'.

mangerlahn
  • 4,746
  • 2
  • 26
  • 50
Eyal Redler
  • 135
  • 7

0 Answers0