Bar Button Element misaligned

I have a problem with my navigation bar, UIBarButtonItem is not aligned vertically. I don't know why, because I'm using a simple UIBarButtonItem, not a custom view. See my code below and thanks for your help!

UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithTitle:@"New Game" style:UIBarButtonItemStyleDone target:self action:@selector(newGame)]; self.navigationItem.rightBarButtonItem = newGameItem; 

Screenshot navigation bar issue

+6
source share
4 answers

There are actually several ways to align panel buttons. Try adjusting the position of the button name as follows:

 [self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10, -10) forBarMetrics:UIBarMetricsDefault]; 

-10 -10 - for example,

OR

you can initialize your newGameItem with a custom UIButton . The user button is a subclass of UIButton with an overridden method

 - (UIEdgeInsets)alignmentRectInsets { return UIEdgeInsetsMake(0, -10, 0, 15.0f); // here you can play with values to align your button properly } 

then initialize newGameItem

 CustomBarButton *button = [CustomBarButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"New game" forState:UIControlStateNormal]; button.frame = CGRectMake (0, 0, 60, 40); [button addTarget:self action:@selector(newGame) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.rightBarButtonItem = newGameItem; 
+6
source

I had the same problem with the button on the left, I was able to fix it in Xcode 6.3.1: select the button (panel item). Right Column Tab Ruler Tab. Upper value: -54 instead of 0. Note that: my device is iPhone4 with iOS 7, the button was completely in place in Xcode and inappropriate on the device, now it is installed on the device and inappropriate in Xcode

+1
source

I had a similar problem: we use custom fonts through UIAppearance for navigation headers and panel elements, and in some cases the correct element was vertically shifted.

I could easily correct misalignment using [self.navigationItem setRightBarButtonItem:rightItem animated:YES] instead of setting properties.

+1
source

Unfortunately, you cannot do UIBarButtonItem with a vertical spacing. I suppose you have to either increase the font size of your title on the UINavigationBar (you can do this using UIAppearance ), or reduce the font size of your UIBarButtonItem .

0
source

All Articles