条形按钮项目未正确对齐

时间:2023-01-16 12:24:26

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

我的导航栏有问题,UIBarButtonItem没有正确垂直对齐。我不知道为什么,因为我使用的是简单的UIBarButtonItem,而不是自定义视图。请参阅下面的代码并感谢您的帮助!

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

条形按钮项目未正确对齐

4 个解决方案

#1


7  

Actually there are couple ways to align the bar buttons. Try to adjust the button title position with this:

实际上有几种方法可以对齐条形按钮。尝试使用以下方法调整按钮标题位置:

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

-10 -10 - just for example

-10 -10 - 仅举例

OR

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

您可以使用自定义UIButton初始化newGameItem。自定义按钮是UIButton的子类,此方法被覆盖

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

then initialize your newGameItem

然后初始化你的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;

#2


1  

I had the same problem with a button on the left, I was able to fix it in Xcode 6.3.1: select the button (bar button item) right column tab "ruler" inset image Top value: -54 instead of 0. Note that: my device is an iPhone4 with iOS 7, the button was perfectly in place in Xcode and misplaced on the device, now it is in place on the device and misplaced in Xcode

我在左边的按钮上遇到了同样的问题,我能够在Xcode 6.3.1中修复它:选择按钮(条形按钮项目)右列选项卡“标尺”插入图像顶部值:-54而不是0.注意那个:我的设备是带有iOS 7的iPhone4,按钮在Xcode中完全就位并放错在设备上,现在它已放置在设备上并放错在Xcode中

#3


1  

I had a similar issue: We use custom fonts via UIAppearance for both navigation titles and bar button items, and in some cases, the right item was vertically misaligned.

我有一个类似的问题:我们通过UIAppearance为导航标题和条形按钮项使用自定义字体,在某些情况下,正确的项目是垂直未对齐的。

I could easily fix the misalignment by using [self.navigationItem setRightBarButtonItem:rightItem animated:YES] instead of the property setter.

我可以通过使用[self.navigationItem setRightBarButtonItem:rightItem animated:YES]而不是属性setter来轻松修复错位。

#4


0  

Unfortunately, there is not much you can do about UIBarButtonItem vertical spacing. I believe you'll have to either increase the font size of your title on the UINavigationBar (you can do this with UIAppearance also), or decrease the font size of your UIBarButtonItem.

不幸的是,关于UIBarButtonItem垂直间距,你无能为力。我相信您必须在UINavigationBar上增加标题的字体大小(您也可以使用UIAppearance执行此操作),或者减小UIBarButtonItem的字体大小。

#1


7  

Actually there are couple ways to align the bar buttons. Try to adjust the button title position with this:

实际上有几种方法可以对齐条形按钮。尝试使用以下方法调整按钮标题位置:

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

-10 -10 - just for example

-10 -10 - 仅举例

OR

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

您可以使用自定义UIButton初始化newGameItem。自定义按钮是UIButton的子类,此方法被覆盖

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

then initialize your newGameItem

然后初始化你的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;

#2


1  

I had the same problem with a button on the left, I was able to fix it in Xcode 6.3.1: select the button (bar button item) right column tab "ruler" inset image Top value: -54 instead of 0. Note that: my device is an iPhone4 with iOS 7, the button was perfectly in place in Xcode and misplaced on the device, now it is in place on the device and misplaced in Xcode

我在左边的按钮上遇到了同样的问题,我能够在Xcode 6.3.1中修复它:选择按钮(条形按钮项目)右列选项卡“标尺”插入图像顶部值:-54而不是0.注意那个:我的设备是带有iOS 7的iPhone4,按钮在Xcode中完全就位并放错在设备上,现在它已放置在设备上并放错在Xcode中

#3


1  

I had a similar issue: We use custom fonts via UIAppearance for both navigation titles and bar button items, and in some cases, the right item was vertically misaligned.

我有一个类似的问题:我们通过UIAppearance为导航标题和条形按钮项使用自定义字体,在某些情况下,正确的项目是垂直未对齐的。

I could easily fix the misalignment by using [self.navigationItem setRightBarButtonItem:rightItem animated:YES] instead of the property setter.

我可以通过使用[self.navigationItem setRightBarButtonItem:rightItem animated:YES]而不是属性setter来轻松修复错位。

#4


0  

Unfortunately, there is not much you can do about UIBarButtonItem vertical spacing. I believe you'll have to either increase the font size of your title on the UINavigationBar (you can do this with UIAppearance also), or decrease the font size of your UIBarButtonItem.

不幸的是,关于UIBarButtonItem垂直间距,你无能为力。我相信您必须在UINavigationBar上增加标题的字体大小(您也可以使用UIAppearance执行此操作),或者减小UIBarButtonItem的字体大小。