条形按钮项目未正确对齐 [英] Bar Button Item not correctly aligned

查看:71
本文介绍了条形按钮项目未正确对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 *newGameItem = [[UIBarButtonItem alloc] initWithTitle:@"New Game" style:UIBarButtonItemStyleDone target:self action:@selector(newGame)];
self.navigationItem.rightBarButtonItem = newGameItem;

推荐答案

实际上,有几种方法可以对齐条形按钮. 尝试通过以下方法调整按钮标题的位置:

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-例如

-10 -10 - just for example

OR

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

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

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

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

这篇关于条形按钮项目未正确对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆