1

I'm writing an app in objective C for ios 8, and my navigation bar has an annoying margin thats causing it to be shifted to the right ~20 pixels for leftBarButtonItem and ~20 left for rightBarButtonItem, as shown in the image below. I have tried using negative spacers per some other posts to no avail. Any ideas?

(I'm trying to do this without using storyboards or xibs.)

enter image description here

Here is the code:

UIToolbar* toolbar_temp = [[UIToolbar alloc] init];
toolbar_temp.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45);
toolbar_temp.barTintColor= [UIColor blackColor];

NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIImage *camimage = [UIImage imageNamed:@"cancel"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:camimage style:UIBarButtonItemStylePlain    target:self     action:nil];

[items addObject:spaceItem ];
[items addObject:customItem ];
[items addObject:spaceItem ];

[toolbar_temp setItems:items animated:NO];
UIBarButtonItem *allButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar_temp];
self.navigationItem.leftBarButtonItem = allButton;

EDIT: I'm not sure if this gives any hints, but for some reason this works:

[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];

While this does not:

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

SOLUTION: I ended up finding the solution myself. The issue was that I have a tabbar controller which had a navigation controller as one of the views. The navigation controller can only be setup in viewDidload, which doesn't get called in tabbar controllers. The solution was to add viewDidAppear and add the navigation controller within that function.

- (void)viewDidAppear:(BOOL)animated{[self addToolbar];}

Thanks all for the help!

ABC
  • 665
  • 1
  • 6
  • 15
  • Which view is it being added to? – dave234 May 23 '15 at 03:36
  • The app is a tabbed view controller, and this is in the first view which is set up to be a navigation controller. When I tried to debug, I set it up to be a regular toolbar instead of a navigation toolbar and it worked fine. (If that helps at all.) – ABC May 23 '15 at 03:50
  • I'd like to see how you add the UIToolbar to your view. I think you might need to either hide the default one that comes with the navigation controller, or add the bar button items directly to the default one instead of adding a toolbar. Maybe if you show a little more of your setup, we can spot where that weird 20px is coming from. – dave234 May 23 '15 at 04:43
  • Ok, so I did a bit more debugging. I'm setting up the images for the tab bar items by initializing with "initWithNibName". When I do this it seems I can not properly set the items in the navigation bar, but I can properly initialize the tabbar items. To make it more confusing I can properly initialize the navigation bar if I don't have "initWithNibName"- in this case viewdidload gets called and the navigation items are as they should be, but then my tab bar items are not properly initialized. Does this sufficiently help or should I post some more code? – ABC May 23 '15 at 04:56

1 Answers1

0

It seems you cannot alter the minimum margins. You can do is add the toolbar_temp to the navigation bar:

[self.navigationController.navigationBar addSubview:toolbar_temp];

Or add to the self.view:

self.navigationController.navigationBarHidden = YES;
[self.view addSubview:toolbar_temp];
Community
  • 1
  • 1
Bannings
  • 10,376
  • 7
  • 44
  • 54