ios – 条形按钮项目未正确对齐

我的导航栏有问题,UIBarButtonItem没有正确垂直对齐.我不知道为什么,因为我使用的是简单的UIBarButtonItem,而不是自定义视图.请参阅下面的代码并感谢您的帮助!
UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithTitle:@"New Game" style:UIBarButtonItemStyleDone target:self action:@selector(newGame)];
self.navigationItem.rightBarButtonItem = newGameItem;

解决方法

实际上有几种方法可以对齐条形按钮.
尝试使用以下方法调整按钮标题位置:
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10,-10) 
                                                     forBarMetrics:UIBarMetricsDefault];

-10 -10 – 仅举例

要么

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

- (UIEdgeInsets)alignmentRectInsets {
    return UIEdgeInsetsMake(0,-10,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,60,40);
[button addTarget:self action:@selector(newGame) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = newGameItem;

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...