Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButton Item on screen edge in iOS 8 when used in standalone view

In my UINavigation bar added to a XIB with a number of UIViews the positioning of the left and right bar button item is way off:

Navigation bar in additiona UIView in XIB

The view in the XIB simply has an outlet to a view controller, but is not the main view. It's shown via:

[UIView transitionFromView:self.view toView:self.settingsView duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

I have another simpler view which is directly bound to the view property of a view controller which - as expected - looks just normal.

Normal navigation bar

All the views have auto layout. The constraints are fine. I tried a number of different things, but couldn't come up with a fix (or a reason, for that matter). The navigation bar and items are just plain vanilla bar button items without anything like appearance proxies etc...

In the 7.1 sim everything looks just normal.

Anyone seen this before?

Thanks

[EDIT]

I found a solution but not the reason:

If I instead of

[UIView transitionFromView:self.view toView:self.settingsView duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

which adds the new view as a subview of the UIWindow,

use this:

[UIView transitionWithView:self.view duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
    [self.view addSubview:self.settingsView];
} completion:nil];

which adds the new view as a subview of the existing UIViewControllers view, everything is fine.

I wonder if this a bug and UINavigationBars render in a strange way if contained in a view that is dynamically added to the window...

like image 1000
k1th Avatar asked Oct 20 '14 22:10

k1th


1 Answers

Not sure whats happening with UIBarButtonItem. It should be arrange by auto layout and it has to be work well. May be one of the constraint conflict with other or misguided.

If you are not able to resolve it. I have one more solution for you. You need to create UIBarButtonItem programatically in your VC.

The idea is to assign space from left and right padding before you add UIBarButtonItem.

Below code will guide you to do the trick.

UIBarButtonItem *leftPadding = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                UIBarButtonSystemItemFixedSpace target:self action:nil];
[leftPadding setWidth:5];  // Adjust width for padding from left side.

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                              UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped:)];
[self.navigationItem setLeftBarButtonItems:@[leftPadding, addButton]];


UIBarButtonItem *rightPadding = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                 UIBarButtonSystemItemFixedSpace target:self action:nil];
[rightPadding setWidth:10]; // Adjust width for padding from right side.

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                               UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped:)];

[self.navigationItem setRightBarButtonItems:@[rightPadding, doneButton]];
like image 50
Kampai Avatar answered Sep 23 '22 03:09

Kampai