在iOS7下,默认导航栏背景,颜色是这样的,接下来咱们就进行自定义,若是你仅仅是更改一下背景和颜色,代码会很简单,不须要很复杂的自定义View来替代leftBarItem 网络
方法一: app
咱们把背景改为了蓝色,title文字改为了白色,是否是很简单呢?NavigationBar极其push过去的子页面也会是你修改后的背景颜色 ide
方法二: 字体
若是您的应用程序使用了自定义图像做为栏的背景,你须要提供一个“更大”的图片,使其延伸了状态栏的后面。导航栏的高度如今是从44点(88像素)更改成64点(128像素)。
仍然可使用了setBackgroundImage:方法来指定自定义图像的导航栏。下面是代码行设置背景图片: ui
效果图和上面的同样,我就不贴出来了。 spa
就像iOS 6,咱们能够经过使用导航栏的“titleTextAttributes”属性来自定义的文本样式。能够指定字体,文字颜色,文字阴影颜色,文字阴影在文本标题偏移属性字典,使用下面的文本属性键: .net
UITextAttributeFont - 字体
UITextAttributeTextColor - 文字颜色
UITextAttributeTextShadowColor - 文字阴影颜色
UITextAttributeTextShadowOffset - 偏移用于文本阴影 code
不想标题栏是光秃秃的文字?能够经过使用代码行中的图像或标志取代它:简单地改变titleview用来自定义,(适用于较低版本) orm
效果以下,我随便用了个图片,别介意: blog
还有一个例子:
CGRect rect = [UIScreen mainScreen].bounds;
_friendsCircleTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,140, 29)];
_friendsCircleTitleView.backgroundColor = [UIColor clearColor];
_friendsCircleTitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
_friendsCircleTitleView.autoresizesSubviews = YES;
_sameCityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_sameCityBtn.backgroundColor = [UIColor clearColor];
_sameCityBtn.frame = CGRectMake(0, 0, 70, 29);
[_sameCityBtn setTitle: @"同城" forState: UIControlStateNormal];
[_sameCityBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[_sameCityBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateHighlighted];
_sameCityBtn.titleLabel.font = [UIFont boldSystemFontOfSize: 13.0];
[_sameCityBtn setBackgroundImage:[UIImage imageNamed:@"sameCity"] forState:UIControlStateNormal];
[_sameCityBtn setBackgroundImage:[UIImage imageNamed: @"sameCitySelected"] forState:UIControlStateHighlighted];
[_sameCityBtn setBackgroundImage:[UIImage imageNamed: @"sameCitySelected"] forState:UIControlStateSelected];
_sameCityBtn.tag = TAG_OF_SAMECITY_BTN;
[_sameCityBtn addTarget:self action:@selector(onLeftTitleBtnPressed)forControlEvents:UIControlEventTouchUpInside];
[_friendsCircleTitleView addSubview: _sameCityBtn];
_sameCityBtn.selected = YES;
_sameCityBtn.userInteractionEnabled = NO;
_wholeCountryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_wholeCountryBtn.backgroundColor = [UIColor clearColor];
_wholeCountryBtn.frame = CGRectMake(_sameCityBtn.frame.origin.x+70, 0, 70, 29);
[_wholeCountryBtn setTitle:@"全国" forState: UIControlStateNormal];
[_wholeCountryBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[_wholeCountryBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateHighlighted];
_wholeCountryBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
[_wholeCountryBtn setBackgroundImage:[UIImage imageNamed: @"wholeCountryNomal"]forState:UIControlStateNormal];
[_wholeCountryBtn setBackgroundImage:[UIImage imageNamed: @"wholeCountrySelected"]forState: UIControlStateHighlighted];
[_wholeCountryBtn setBackgroundImage:[UIImage imageNamed: @"wholeCountrySelected"]forState: UIControlStateSelected];
_wholeCountryBtn.tag = TAG_OF_WHOLECOUNTY_BTN;
[_wholeCountryBtn addTarget:self action:@selector(onRightTitleBtnPressed)forControlEvents:UIControlEventTouchUpInside];
[_friendsCircleTitleView addSubview:_wholeCountryBtn];
_wholeCountryBtn.selected = NO;
self.navigationItem.titleView = _friendsCircleTitleView;
效果如图:您但愿添加导航栏的一侧不止一个栏按钮项目,不管是leftBarButtonItems和rightBarButtonItems 您在导航栏左侧/右侧指定自定义栏按钮项目。好比你想添加一个摄像头和一个共享按钮右侧的吧。您可使用下面的代码:
一般状况下,咱们使用UINavigationController时,push到的子页面,左上角会是系统自动取值上一层父页面的title名称,默认状况是这样,那么咱们该如何修改它呢?
左侧显示了父页面的title:用户登陆,但是咱们想修改为返回,方式有不少,举些例子
方法一:
经过设置navigationItem的backBarButtonItem能够直接更换文字,【注意,要在父视图的Controller中设置】以下:
全部的子界面返回时都变成了咱们定义的文字,若是不想显示文字,直接"",就会单独显示一个系统的返回箭头图标,也是很清晰的感受。
作到这里发现文字颜色和背景有重复,那么如何自定义其颜色呢?在iOS7,能够改变tintColor属性,它提供了一个快速和简单的方式,下面是一个示例代码片断:
全是系统的图标和文字,这回看着舒服了,有木有?【除了后退按钮,请注意,tintColor属性影响全部按钮标题和按钮图像】
最后举个例子,另一种实现自定义导航控制器返回按钮,代码以下:
还有一种方法
UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 30, 30)];
[leftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftBarItem;
效果如图:
最后说一下使用pushViewController切换到下一个视图时,navigation controller按照如下3条顺序更改导航栏的左侧按钮(本段摘自网络):
一、若是B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;
二、若是B没有自定义按钮,可是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;
三、若是前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题;
如何去掉导航栏下面的那条黑线呢?
如图
有条线很难看,在
didFinishLaunchingWithOptions中增长
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
这两行代码就能够了。效果以下: