iOS7 UI兼容 导航栏按钮边距

转载自:http://www.cnblogs.com/maxfong/p/3375167.html

iOS7之前的UI为:

而在iOS7中,由于设计方面的原因,使得UI变为:

修改的方法重写UINavigationItem的setLeftBarButtonItem和setRightBarButtonItem方法,使之与之前版本兼容;

代码如下:


@interface UINavigationItem (margin)

@end


@implementation UINavigationItem (margin)

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
- (void)setLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -12;

if (_leftBarButtonItem)
{
[self setLeftBarButtonItems:@[negativeSeperator, _leftBarButtonItem]];
}
else
{
[self setLeftBarButtonItems:@[negativeSeperator]];
}
[negativeSeperator release];
}
else
{
[self setLeftBarButtonItem:_leftBarButtonItem animated:NO];
}
}

- (void)setRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -12;

if (_rightBarButtonItem)
{
[self setRightBarButtonItems:@[negativeSeperator, _rightBarButtonItem]];
}
else
{
[self setRightBarButtonItems:@[negativeSeperator]];
}
[negativeSeperator release];
}
else
{
[self setRightBarButtonItem:_rightBarButtonItem animated:NO];
}
}

#endif
@end

项目源码不做任何修改,问题解决;

添加Button:


UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = leftBarItem;
[leftBarItem release];

iOS7 UI兼容 导航栏按钮边距,布布扣,bubuko.com

时间: 2024-10-12 21:30:36

iOS7 UI兼容 导航栏按钮边距的相关文章

IOS7怎么修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

我想设置Navigation Bar的背景颜色为黑色,然后所有内部颜色为白色. 因此,我用了这段代码: 1 [[UINavigationBar appearance] setTitleTextAttributes: 2 [NSDictionary dictionaryWithObjectsAndKeys: 3 [UIColor whiteColor], 4 NSForegroundColorAttributeName, 5 [UIColor whiteColor], 6 NSForeground

实际iOS编程中遇到的自定义导航栏按钮,导致手势返回失效的解决方法

1\在实际编程过程中往往需要自定义导航栏上面的按钮,也就用: - (instancetype)initWithCustomView:(UIView *)customView; 但用了这个方法后可能会导致iOS7,8的手势返回失效,解决方法就是在自定义的导航栏的viewDidLoad方法中添加如下代码 注意:只有用系统的导航栏,或者继承于系统的导航栏才可以用Push方法,并且自带返回手势. - (void)viewDidLoad { [super viewDidLoad]; __weak type

自定义导航栏按钮

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"侧栏" style:UIBarButtonItemStylePlain target:self action:@selector(clickleftButton:)]; 如果我们使用这行代码来定义按钮,无论左边右边,图片还是文字,他都会把字体或图片颜色变成默认的蓝色,然而并不是我们的需求, 所以我们这样 //导航栏 double

iOS 开发之 - iOS6适配 - 导航栏按钮透明方法

首先上张图: 1:ios6导航栏默认按钮 rightBarButtonItem   是不是很丑的赶脚? 现在通过以下方法来改变以下:code: UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(0, 0, 40, 40); [rightButton setTitle:@"提交" forState:UIControlStateNormal

导航栏按钮

? 1 2 3 4 5 6 7 8 9 10 11 12 13 //导航栏按钮    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成"                                                                   style:UIBarButtonItemStylePlain                              

Android Fragment解析及UI底部导航栏实例

import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import and

iOS 笔记-自定义的导航栏按钮

暂时记录一个小知识点,因为赶着做项目,后续会慢慢补充 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"侧栏" style:UIBarButtonItemStylePlain target:self action:@selector(clickleftButton:)]; 如果我们使用这行代码来定义按钮,无论左边右边,图片还是文字,他都会把字体或图片颜色变成默认的蓝色,然而并不

自定义导航栏按钮 baseViewController中写!

// 自定义导航栏"返回按钮" - (void)initNaviBackBtn {     UIButton *backBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];     backBtn.frame = CGRectMake(0, 0, 50, 22);     [backBtn setImage:[UIImageimageNamed:@"backBtn"] forState:UIControlStateNo

ios7以上自定义导航栏标题的字体大小及颜色的方法

自定义导航栏的字体和颜色,只需要自定义一个lable,然后将lable添加到导航栏的titleview中就可以了 代码如下 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size. [label setNumberOfLines:0]; UIFont *font = [UIFont fontWithName:@"Arial" size:17]