导航栏中的 UIBarButtonItem 设置垂直位置偏移

Posted

技术标签:

【中文标题】导航栏中的 UIBarButtonItem 设置垂直位置偏移【英文标题】:UIBarButtonItem in navigationBar set Vertical Position Offset 【发布时间】:2016-11-21 18:26:57 【问题描述】:

我正在尝试“向下”移动 UINavigationBar 中的 UIBarButtonItem。为此,我使用以下代码:

[[self.navigationItem.rightBarButtonItems objectAtIndex:0] setTitlePositionAdjustment:UIOffsetMake(0.0f, 10.0f) forBarMetrics:UIBarMetricsDefault];

如果我将水平位置的 0.0f 更改为任意值,则文本会水平移动。但我不能改变垂直位置。无论我为 10.0f 输入什么,我都看不到任何效果。

编辑:我已将 UIView 添加到我的 UINavigationar。 [self.navigationController.navigationBar addSubview:statusbarview];

【问题讨论】:

【参考方案1】:

您需要使用setBackgroundVerticalPositionAdjustment 来调整您的 UIBarButton

根据文档

此偏移量用于调整边框条的垂直居中 栏中的按钮。

这里是reference

这是一个例子

[yourBarButton setBackgroundVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault];

【讨论】:

不幸的是,这也没有效果。也许我必须添加信息,即我向导航栏添加了 UIView? (self.navigationController.navigationBar) statusbarview中添加按钮了吗? 不,在 UINavigationBar 上,但也许它有我认为的效果 我问你添加 UIView 作为 UIBarButton,在那个视图中你添加按钮了吗? 没有。我在 InterfaceBuilder 中的 NavigationBar 中添加了一个 UIBarButtonItem。稍后在 viewDidLoad 中,我还将 UIView 添加到 NavigationBar

以上是关于导航栏中的 UIBarButtonItem 设置垂直位置偏移的主要内容,如果未能解决你的问题,请参考以下文章