微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

原文地址:https://www.jb51.cc/iOS/334441.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐