Ios 调整UINavigationBar中的按钮按钮位置
我需要调整UINavigationBar中我的uibarbuttonite的垂直位置。我认为有一种方法可以做到这一点。将UIView添加到UIBarButtonim,并将UIButton添加到UIView。如果您只有一个或两个导航栏就可以了。但我认为如果你有几十个UINavigationBar,特别是在故事板里面,那就有点太麻烦了。所以我做了一些研究,找到了一个简单的解决办法。即使用类别。以下是我的源代码:Ios 调整UINavigationBar中的按钮按钮位置,ios,layout,uinavigationbar,uinavigationitem,Ios,Layout,Uinavigationbar,Uinavigationitem,我需要调整UINavigationBar中我的uibarbuttonite的垂直位置。我认为有一种方法可以做到这一点。将UIView添加到UIBarButtonim,并将UIButton添加到UIView。如果您只有一个或两个导航栏就可以了。但我认为如果你有几十个UINavigationBar,特别是在故事板里面,那就有点太麻烦了。所以我做了一些研究,找到了一个简单的解决办法。即使用类别。以下是我的源代码: #pragma clang diagnostic push #pragma clang
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(void)viewWillAppear:(BOOL)animated
{
[self show:NO];
}
// do your override
-(void)viewWillLayoutSubviews
{
[self changeTheButtonPosition:self.navigationItem.leftBarButtonItem];
[self changeTheButtonPosition:self.navigationItem.rightBarButtonItem];
}
-(void)viewDidAppear:(BOOL)animated
{
[self show:YES];
}
#pragma clang diagnostic pop
-(void)show:(BOOL)show
{
self.navigationItem.leftBarButtonItem.customView.hidden = !show;
self.navigationItem.rightBarButtonItem.customView.hidden = !show;
}
- (void)changeTheButtonPosition:(UIBarButtonItem *)barButtonItem
{
if ( barButtonItem )
{
UIView* customView = barButtonItem.customView;
// NSLog(@"custom view frame = %@",NSStringFromCGRect(customView.frame));
CGRect frame = customView.frame;
CGFloat y = SYSTEM_VERSION_LESS_THAN(@"7") ? 10.0f : 5.0f;
customView.frame = CGRectMake(frame.origin.x, y, frame.size.width, frame.size.height);
}
}
它在iOS 7和iOS 6的第一视图控制器中都能完美工作。但它不适用于iOS 6中的UIViewController。我找不到任何理由。有人能提供建议吗?iOS 6中我的代码出了什么问题?您应该能够使用iOS 5中引入的
UIAppearance
代理方法来实现这一点。特别是:
- (void)setBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics
- (void)setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics
查看
uibarbuttoneim
了解更多信息。我在中找到了另一个解决方案,尽管它比我的解决方案要困难一些。但是,这在iOS 7中似乎不太管用。我在设置标题位置时遇到一些问题,然后操作系统接管,任何其他调整都会导致按钮移动。任何方向都会移动它。最终目的地是默认位置。具体来说,我有一个编辑按钮的问题,并试图将其固定到一个更高的导航栏顶部。