UINavigationBar内部垂直居中的元素,具有自定义高度 [英] Center elements vertically inside UINavigationBar with custom height
问题描述
我正在开发一个iOS 6和7应用程序,要求导航栏比通常的44/64分高出
我已经搜索过高和低,到目前为止看起来最干净的解决方案是使用一个类别(或子类)并实现 - (CGSize)sizeThatFits:(CGSize)size
方法返回不同的大小。
I'm developing an iOS 6 and 7 app that requires the navigation bar to be taller than the usual 44/64 pts
I've searched high and low and so far it seems the cleanest solution is to use a category (or subclass) and implement the - (CGSize)sizeThatFits:(CGSize)size
method to return a different size.
这样做效果很好,但这样做会导致里面的所有项目都停留在导航栏的底部,而我想让它们居中。
我尝试使用Appearance Proxy协议来定义按钮的垂直偏移,特别是这段代码
This works fine in just making , however doing this causes all the items inside to rest at the bottom of the navigation bar, while I'd like to have them centered. I have tried using the Appearance Proxy protocol to define vertical offsets for the buttons, specifically this code
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment: -10 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment: -10 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment: -10 forBarMetrics: UIBarMetricsDefault];
在iOS 6上运行正常,产生此结果
Works just fine on iOS 6, producing this result
但不是'在iOS 7上工作,改为给我这个。
But doesn't work on iOS 7, giving me this instead.
我已经在iOS 7下阅读了这些调整仅在使用自定义视图时有效但对我来说似乎很奇怪,特别是考虑到 setBackButtonTitlePositionAdjustment:forBarMetrics:
实际上将后退按钮的文本向上移动,但不会移动V形符号并且标题实际上会轻推。
I have read around that under iOS 7 the adjustments only work when using custom views but it seems odd to me, especially considering that setBackButtonTitlePositionAdjustment:forBarMetrics:
actually moves the text for the back button up, but not the chevron and that the title actually does nudge up.
我也尝试过一个子类化路由,使用layoutSubviews向下移动UINavigationBar中的视图。
这样可行,但当按下新的视图控制器时,按钮会在过渡期间跳转。
I've also tried going for a subclassing route, using layoutSubviews to move down the views inside the UINavigationBar. This works, but of course when a new view controller is pushed the buttons jump around during the transition.
我错过了什么吗?提前致谢
Am I missing something? Thanks in advance
更新我已经编辑了说明,以便更清楚我的问题在于栏内的内容,而不是栏本身。
UPDATE I have edited the description to make it clearer that my issue is with what's inside the bar, not the bar itself.
推荐答案
我发现这个问题最干净的解决方案是使用我自己的 UINavigationBar的子类
并通过覆盖 layoutSubviews
方法垂直居中:
The cleanest solution I've found to this problem was to use my own subclass of UINavigationBar
and center the buttons vertically by overriding the layoutSubviews
method:
- (void)layoutSubviews
{
[super layoutSubviews];
NSArray *subviews = self.subviews;
for (UIView *view in subviews) {
if ([view isKindOfClass:[UIButton class]]) {
view.frame = ({
CGRect frame = view.frame;
CGFloat navigationBarHeight = CGRectGetHeight(self.frame);
CGFloat buttonHeight = CGRectGetHeight(view.frame);
frame.origin.y = (navigationBarHeight - buttonHeight) / 2.0f;
frame;
});
}
}
}
制作 UINavigationController
使用你的 UINavigationBar
的子类你可以使用 initWithNavigationBarClass:toolbarClass:
初始化:
To make a UINavigationController
use your subclass of UINavigationBar
you can use the initWithNavigationBarClass:toolbarClass:
initialiser:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:nil];
这篇关于UINavigationBar内部垂直居中的元素,具有自定义高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!