ios 导航栏弹出防盗号提示会让标题栏和导航栏的区别向右偏移是为什么

IOS--导航栏(11)
A.NavigationBar标题按钮
在“首页”的导航栏中部设置一个“首页”文字+箭头按钮统一设置样式根据实际文本长度调整宽度消除系统自带的点击高亮效果点击按钮,箭头上下颠倒
使用UIButton,设置文本和图片在initWithFrame统一样式创建一个继承UIButton的自定义类,重写文本和图片的绘图方法,互换位置设置一个标识成员变量,判断当前的按钮状态(弹出 or 缩回)
(1)自定义继承UIButton的类 HVWNavigationBarTitleButton
HVWNavigationBarTitleButton.m
Created by hellovoidworld on 15/2/2.
Copyright (c) 2015年 hellovoidworld. All rights reserved.
9 #import &HVWNavigationBarTitleButton.h&
<span style="color:#
<span style="color:# @implementation HVWNavigationBarTitleButton
<span style="color:#
<span style="color:# /** 重写initWithFrame, 统一设置按钮的样式 */
<span style="color:# - (instancetype)initWithFrame:(CGRect)frame {
<span style="color:#
if (self = [super initWithFrame:frame]) {
<span style="color:#
// 设置字体
<span style="color:#
self.titleLabel.font = HVWNavigationTitleF
<span style="color:#
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
<span style="color:#
<span style="color:#
// 文本右对齐
<span style="color:#
[self.titleLabel setTextAlignment:NSTextAlignmentRight];
<span style="color:#
<span style="color:#
// 取消图标高亮效果
<span style="color:#
[self setAdjustsImageWhenDisabled:NO];
<span style="color:#
<span style="color:#
// 图片居中
<span style="color:#
[self.imageView setContentMode:UIViewContentModeCenter];
<span style="color:#
<span style="color:#
<span style="color:#
<span style="color:#
<span style="color:# }
<span style="color:#
<span style="color:# /** 重写iamge的绘图方法 */
<span style="color:# - (CGRect)imageRectForContentRect:(CGRect)contentRect {
<span style="color:#
CGFloat height = contentRect.size.
<span style="color:#
CGFloat width =
<span style="color:#
CGFloat x = self.size.width -
<span style="color:#
CGFloat y = <span style="color:#;
<span style="color:#
return CGRectMake(x, y, width, height);
<span style="color:# }
<span style="color:#
<span style="color:# /** 重写title的绘图方法 */
<span style="color:# - (CGRect)titleRectForContentRect:(CGRect)contentRect {
<span style="color:#
CGFloat height = contentRect.size.
<span style="color:#
// 文本宽度 = 按钮整体宽度 - 图片宽度
<span style="color:#
CGFloat width = self.height -
<span style="color:#
CGFloat x = <span style="color:#;
<span style="color:#
CGFloat y = <span style="color:#;
<span style="color:#
return CGRectMake(x, y, width, height);
<span style="color:# }
<span style="color:#
<span style="color:# @end
(2)在“首页”控制器设置“标题按钮”:
HVWHomeViewController.m:
1 - (void)viewDidLoad {
[super viewDidLoad];
// 添加导航控制器按钮
// 左边按钮
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@&navigationbar_friendsearch& hightlightedImage:@&navigationbar_friendsearch_highlighted& target:self selector:@selector(searchFriend)];
// 右边按钮
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@&navigationbar_pop& hightlightedImage:@&navigationbar_pop_highlighted& target:self selector:@selector(pop)];
<span style="color:#
<span style="color:#
// 设置标题按钮
<span style="color:#
HVWNavigationBarTitleButton *titleButton = [[HVWNavigationBarTitleButton alloc] init];
<span style="color:#
titleButton.height = <span style="color:#;
<span style="color:#
titleButton.width = <span style="color:#0;
<span style="color:#
[titleButton setTitle:@&首页& forState:UIControlStateNormal];
<span style="color:#
[titleButton setImage:[UIImage imageWithNamed:@&navigationbar_arrow_down&] forState:UIControlStateNormal];
<span style="color:#
// 设置背景图片
<span style="color:#
[titleButton setBackgroundImage:[UIImage resizedImage:@&navigationbar_filter_background_highlighted&] forState:UIControlStateHighlighted];
<span style="color:#
<span style="color:#
// 监听按钮点击事件,替换图标
<span style="color:#
[titleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];
<span style="color:#
<span style="color:#
self.navigationItem.titleView = titleB
<span style="color:# }
<span style="color:#
<span style="color:# /** 标题栏按钮点击事件 */
<span style="color:# - (void) titleButtonClickd:(UIButton *) button {
<span style="color:#
self.titleButtonExtended = !self.titleButtonE
<span style="color:#
<span style="color:#
if (self.isTitleButtonExtended) {
<span style="color:#
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_up&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:#
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_down&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:# }
<span style="color:#
<span style="color:# #mark:有希望依赖图片缓存,使用&==&判断当前按钮图片来决定按钮状态的,发现使用currentImage和新建一个UIImage(同一张图片)出来的地址并不一致!所以不能采用。
<span style="color:# -(void)titleButtonClick:(UIButton *)titleButton
<span style="color:# {
<span style="color:#
UIImage *titleImage=[UIImage imageWithName:@&navigationbar_arrow_down&];
<span style="color:#
<span style="color:#
if (titleButton.currentImage==titleImage) {
<span style="color:#
//换成箭头向上
<span style="color:#
[titleButton setImage:[UIImage imageWithName:@&navigationbar_arrow_up&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:#
<span style="color:#
//换成箭头向下
<span style="color:#
[titleButton setImage:[UIImage imageWithName:@&navigationbar_arrow_down&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:# }
B.导航栏标题按钮弹出框
点击导航栏标题按钮弹出一个框
点击框外的其他地方,隐藏此框
因为框的范围涉及到了导航栏,所以不能放在导航栏下的内容界面控制器上,要放在导航栏上
在框和导航栏的夹层放置一个全屏的(透明/半透明)的UIButton,用来监听框外点击
封装此功能,可以作为一个弹出菜单控件
(1)简单尝试直接在窗口上添加一个UIImageView
HVWHomeViewController:
1 /** 标题栏按钮点击事件 */
2 - (void) titleButtonClickd:(UIButton *) button {
self.titleButtonExtended = !self.titleButtonE
if (self.isTitleButtonExtended) {
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_up&] forState:UIControlStateNormal];
UIView *window = [[UIApplication sharedApplication] keyWindow];
<span style="color:#
UIImageView *popView = [[UIImageView alloc] init];
<span style="color:#
popView.size = CGSizeMake(<span style="color:#0, <span style="color:#0);
<span style="color:#
popView.centerX = window.width * <span style="color:#.5;
<span style="color:#
popView.y = <span style="color:#;
<span style="color:#
popView.image = [UIImage resizedImage:@&popover_background&];
<span style="color:#
[self.navigationController.view addSubview:popView];
<span style="color:#
<span style="color:#
<span style="color:#
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_down&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:# }
(2)点击其他区域,隐藏弹出框
使用一个全屏透明/半透明UIButton夹在弹出框和底层的控件之间,监听点击事件
1 /** 标题栏按钮点击事件 */
2 - (void) titleButtonClickd:(UIButton *) button {
self.titleButtonExtended = !self.titleButtonE
if (self.isTitleButtonExtended) {
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_up&] forState:UIControlStateNormal];
UIView *window = [[UIApplication sharedApplication] keyWindow];
<span style="color:#
// 中间辅助覆盖层(帮助隐藏弹出框)
<span style="color:#
UIButton *coverLayer = [UIButton buttonWithType:UIButtonTypeCustom];
<span style="color:#
coverLayer.frame = window.
<span style="color:#
coverLayer.backgroundColor = [UIColor blackColor];
<span style="color:#
coverLayer.alpha = <span style="color:#.2;
<span style="color:#
[window addSubview:coverLayer];
<span style="color:#
[coverLayer addTarget:self action:@selector(coverLayerClicked:) forControlEvents:UIControlEventTouchUpInside];
<span style="color:#
<span style="color:#
<span style="color:#
UIImageView *popView = [[UIImageView alloc] init];
<span style="color:#
self.popView = popV
<span style="color:#
popView.size = CGSizeMake(<span style="color:#0, <span style="color:#0);
<span style="color:#
popView.centerX = window.width * <span style="color:#.5;
<span style="color:#
popView.y = <span style="color:#;
<span style="color:#
popView.userInteractionEnabled = YES;
<span style="color:#
<span style="color:#
popView.image = [UIImage resizedImage:@&popover_background&];
<span style="color:#
[window addSubview:popView];
<span style="color:#
<span style="color:#
<span style="color:#
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_down&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:# }
<span style="color:#
<span style="color:# /** 辅助覆盖层点击事件 */
<span style="color:# - (void) coverLayerClicked:(UIButton *) button {
<span style="color:#
if (self.isTitleButtonExtended) {
<span style="color:#
[button removeFromSuperview];
<span style="color:#
[self.popView removeFromSuperview];
<span style="color:#
[self titleButtonClickd:self.titleButton];
<span style="color:#
<span style="color:# }
(3)由于弹出框功能可能在多处用到,而且让控制器负责创建不合适,所以封装成一个类
封装成“弹出菜单”类HVWPopMenu(继承UIView)
内部包含:
遮盖UIButton
一个内容界面 (如tableViewController),作为背景图片的子控件
HVWPopMenu.h
Created by hellovoidworld on 15/2/2.
Copyright (c) 2015年 hellovoidworld. All rights reserved.
9 #import &UIKit/UIKit.h&
<span style="color:#
<span style="color:# typedef enum {
<span style="color:#
PopMenuArrowLeft,
<span style="color:#
PopMenuArrowMid,
<span style="color:#
PopMenuArrowRight
<span style="color:# } PopMenuA
<span style="color:#
<span style="color:# @class HVWPopM
<span style="color:# @protocol HVWPopMenuDelegate &NSObject&
<span style="color:#
<span style="color:# @optional
<span style="color:# - (void) popMenuDidHideMenu:(HVWPopMenu *) popM
<span style="color:#
<span style="color:# @end
<span style="color:#
<span style="color:# @interface HVWPopMenu : UIView
<span style="color:#
<span style="color:# /** 背景兼内容容器 */
<span style="color:# @property(nonatomic, strong) UIImageView *backgroundC
<span style="color:#
<span style="color:# #pragma mark - 成员属性
<span style="color:# /** 遮盖夹层 */
<span style="color:# @property(nonatomic, strong) UIButton *coverL
<span style="color:#
<span style="color:# /** 内容控件 */
<span style="color:# @property(nonatomic, strong) UIView *contentV
<span style="color:#
<span style="color:# /** 箭头位置 */
<span style="color:# @property(nonatomic, assign) PopMenuArrow popMenuA
<span style="color:#
<span style="color:# /** 遮盖夹层透明标识 */
<span style="color:# @property(nonatomic, assign, getter=isDimCoverLayer) BOOL dimCoverL
<span style="color:#
<span style="color:# /** 代理 */
<span style="color:# @property(nonatomic, weak) id&HVWPopMenuDelegate& delegate;
<span style="color:#
<span style="color:#
<span style="color:# #pragma mark - 初始化方法
<span style="color:# - (instancetype) initWithContentView:(UIView *) contentV
<span style="color:# &#43; (instancetype) popMenuWithContentView:(UIView *) contentV
<span style="color:#
<span style="color:# #pragma mark - 使用方法
<span style="color:# /** 弹出 */
<span style="color:# - (void) showMenuInRect:(CGRect)
<span style="color:#
<span style="color:# /** 隐藏 */
<span style="color:# - (void) hideM
<span style="color:#
<span style="color:#
<span style="color:# @end
HVWPopMenu.m
Created by hellovoidworld on 15/2/2.
Copyright (c) 2015年 hellovoidworld. All rights reserved.
9 #import &HVWPopMenu.h&
11 @implementation HVWPopMenu
13 #pragma mark - 初始化方法
14 - (instancetype) initWithContentView:(UIView *) contentView {
if (self = [super init]) {
self.contentView = contentV
22 &#43; (instancetype) popMenuWithContentView:(UIView *) contentView {
return [[self alloc] initWithContentView:contentView];
26 /** 初始化子控件 */
27 - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 中间辅助覆盖层(帮助隐藏弹出框)
UIButton *coverLayer = [UIButton buttonWithType:UIButtonTypeCustom];
self.coverLayer = coverL
[self setDimCoverLayer:YES];
[coverLayer addTarget:self action:@selector(coverLayerClicked) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:coverLayer];
// 添加背景容器
UIImageView *backgroundContainer = [[UIImageView alloc] init];
self.backgroundContainer = backgroundC
backgroundContainer.userInteractionEnabled = YES;
[self setPopMenuArrow:PopMenuArrowMid];
[self addSubview:backgroundContainer];
48 /** 遮盖夹层点击事件 */
49 - (void) coverLayerClicked {
[self hideMenu];
53 #pragma mark - 使用方法
54 /** 弹出 */
55 - (void) showMenuInRect:(CGRect) rect {
// 准备添加到当前主窗口上
UIView *window = [[UIApplication sharedApplication] keyWindow];
self.frame = window.
[window addSubview:self];
self.coverLayer.frame = window.
self.backgroundContainer.frame =
// 添加内容控件
if (self.contentView) {
CGFloat topMargin = <span style="color:#;
CGFloat leftMargin = <span style="color:#;
CGFloat bottomMargin = <span style="color:#;
CGFloat rightMargin = <span style="color:#;
self.contentView.x = leftM
self.contentView.y = topM
self.contentView.width = self.backgroundContainer.width - leftMargin - rightM
self.contentView.height = self.backgroundContainer.height - topMargin - bottomM
[self.backgroundContainer addSubview:self.contentView];
80 /** 隐藏 */
81 - (void) hideMenu {
if ([self.delegate respondsToSelector:@selector(popMenuDidHideMenu:)]) {
[self.delegate popMenuDidHideMenu:self];
[self removeFromSuperview];
89 #pragma mark - 特性设置
90 /** 设置遮盖夹层是否透明 */
91 - (void)setDimCoverLayer:(BOOL)dimCoverLayer {
if (dimCoverLayer) { // 需要半透明模糊效果的
self.coverLayer.backgroundColor = [UIColor blackColor];
self.coverLayer.alpha = <span style="color:#.2;
} else { // 全透明
self.coverLayer.backgroundColor = [UIColor clearColor];
self.coverLayer.alpha = <span style="color:#.0;
<span style="color:#0
<span style="color:#1
<span style="color:#2 /** 设置弹出菜单顶部箭头位置:左、中、右 */
<span style="color:#3 - (void)setPopMenuArrow:(PopMenuArrow)popMenuArrow {
<span style="color:#4
_popMenuArrow = popMenuA
<span style="color:#5
<span style="color:#6
switch (popMenuArrow) {
<span style="color:#7
case PopMenuArrowLeft:
<span style="color:#8
self.backgroundContainer.image = [UIImage resizedImage:@&popover_background_left&];
<span style="color:#9
<span style="color:#0
case PopMenuArrowMid:
<span style="color:#1
self.backgroundContainer.image = [UIImage resizedImage:@&popover_background&];
<span style="color:#2
<span style="color:#3
case PopMenuArrowRight:
<span style="color:#4
self.backgroundContainer.image = [UIImage resizedImage:@&popover_background_right&];
<span style="color:#5
<span style="color:#6
<span style="color:#7
<span style="color:#8
<span style="color:#9 }
<span style="color:#0
<span style="color:#1 @end
HVWHomeViewController.m
2 /** 标题栏按钮点击事件 */
3 - (void) titleButtonClickd:(UIButton *) button {
self.titleButtonExtended = !self.titleButtonE
if (self.isTitleButtonExtended) {
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_up&] forState:UIControlStateNormal];
// 添加弹出菜单
<span style="color:#
UITableView *tableView = [[UITableView alloc] init];
<span style="color:#
HVWPopMenu *popMenu = [HVWPopMenu popMenuWithContentView:tableView];
<span style="color:#
popMenu.delegate =
<span style="color:#
popMenu.dimCoverLayer = YES; // 模糊遮盖
<span style="color:#
popMenu.popMenuArrow = PopMenuArrowM // 中部箭头
<span style="color:#
<span style="color:#
<span style="color:#
[popMenu showMenuInRect:CGRectMake(<span style="color:#, <span style="color:#, <span style="color:#0, <span style="color:#0)];
<span style="color:#
<span style="color:#
<span style="color:#
[button setImage:[UIImage imageWithNamed:@&navigationbar_arrow_down&] forState:UIControlStateNormal];
<span style="color:#
<span style="color:# }
<span style="color:#
<span style="color:# #pragma mark - HVWPopMenuDelegate
<span style="color:# - (void)popMenuDidHideMenu:(HVWPopMenu *)popMenu {
<span style="color:#
UIButton *titleButton = (UIButton *)self.navigationItem.titleV
<span style="color:#
[self titleButtonClickd:titleButton];
<span style="color:# }
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:29001次
积分:1450
积分:1450
排名:千里之外
原创:89篇
转载:157篇
(1)(1)(39)(28)(54)(36)(73)(25)转:ios导航栏设置 - 守望远方 - 博客园
随笔 - 210
原帖:/industry/7.html
本文提供的代码需要用Xcode 5来执行。如果你还在使用老版本的Xcode,那么在运行示例之前请将Xcode升级到Xcode 5。
iOS 7中默认的导航栏
在开始定制之前,我们先来看看iOS 7中默认导航栏的外观。通过Xcode用Single View Controller模板创建一个工程。然后将view controller嵌入到一个navigation controller中。如果你不想从头开始,那么也可以在这里下载到这个。Xcode 5包含有iOS 6和iOS 7模拟器,我们可以在这两个不同的模拟器版本中运行示例程序,进行对比,如下图所示:
如上图所示,在iOS 7中的导航栏默认情况下跟状态栏是交织在一起的,并且它的颜色也被修改为亮灰色。
设置导航栏的背景颜色
在iOS 7中,不再使用tintColor属性来设置导航栏的颜色,而是使用barTintColor属性来修改背景色。我们可以在AppDelegate.m文件中的方法didFinishLaunchingWithOptions:里面添加如下代码来修改颜色:
[[UINavigationBar&appearance]&setBarTintColor:[UIColor&yellowColor]];&
效果如下图所示:
一般情况,我们都会使用自己的颜色,下面这个宏用来设置RGB颜色非常方便:
#define&UIColorFromRGB(rgbValue)&[UIColor&colorWithRed:((float)((rgbValue&&&0xFF0000)&&&&16))/255.0&green:((float)((rgbValue&&&0xFF00)&&&&8))/255.0&blue:((float)(rgbValue&&&0xFF))/255.0&alpha:1.0]&
将上面这个宏放到AppDelegate.m文件中,然后通过这个宏来创建一个UIColor对象(根据指定的RGB)。如下示例:
[[UINavigationBar&appearance]&setBarTintColor:UIColorFromRGB(0x067AB5)];&
默认情况下,导航栏的translucent属性为YES。另外,系统还会对所有的导航栏做模糊处理,这样可以让iOS 7中导航栏的颜色更加饱和。如下图,是translucent值为NO和YES的对比效果:
要想禁用translucent属性,可以在Storyboard中选中导航栏,然后在Attribute Inspectors中,取消translucent的勾选。
在导航栏中使用背景图片
如果希望在导航栏中使用一个图片当做背景,那么你需要提供一个稍微高一点的图片(这样可以延伸到导航栏背后)。导航栏的高度从44 points(88 pixels)变为了64 points(128 pixels)。我们依然可以使用setBackgroundImage:方法为导航栏设置自定义图片。如下代码所示:
[[UINavigationBar&appearance]&setBackgroundImage:[UIImage&imageNamed:@"nav_bg.png"]&forBarMetrics:UIBarMetricsDefault];&
示例工程中提供了两个背景图片:nav_bg.png 和 nav_bg_ios7.png。运行一下试试看吧,如下效果:
定制返回按钮的颜
在iOS 7中,所有的按钮都是无边框的。其中返回按钮会有一个V型箭头,以及上一个屏幕中的标题(如果上一屏幕的标题是空,那么就显示&返回&)。要想给返回按钮着色,可以使用tintColor属性。如下代码所示:
[[UINavigationBar&appearance]&setTintColor:[UIColor&whiteColor]];&
除了返回按钮,tintColor属性会影响到所有按钮标题和图片。
如果想要用自己的图片替换V型,可以设置图片的backIndicatorImage和backIndicatorTransitionMaskImage。如下代码所示:
[[UINavigationBar&appearance]&setBackIndicatorImage:[UIImage&imageNamed:@"back_btn.png"]];&
[[UINavigationBar&appearance]&setBackIndicatorTransitionMaskImage:[UIImage&imageNamed:@"back_btn.png"]];&
图片的颜色是由tintColor属性控制的。
修改导航栏标题的字体
跟iOS 6一样,我们可以使用导航栏的titleTextAttributes属性来定制导航栏的文字风格。在text attributes字典中使用如下一些key,可以指定字体、文字颜色、文字阴影色以及文字阴影偏移量:
UITextAttributeFont & 字体key
UITextAttributeTextColor & 文字颜色key
UITextAttributeTextShadowColor & 文字阴影色key
UITextAttributeTextShadowOffset & 文字阴影偏移量key
如下代码所示,对导航栏的标题风格做了修改:
NSShadow&*shadow&=&[[NSShadow&alloc]&init];&
&&&&shadow.shadowColor&=&[UIColor&colorWithRed:0.0&green:0.0&blue:0.0&alpha:0.8];&
&&&&shadow.shadowOffset&=&CGSizeMake(0,&1);&
&&&&[[UINavigationBar&appearance]&setTitleTextAttributes:&[NSDictionary&dictionaryWithObjectsAndKeys:&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[UIColor&colorWithRed:245.0/255.0&green:245.0/255.0&blue:245.0/255.0&alpha:1.0],&NSForegroundColorAttributeName,&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&shadow,&NSShadowAttributeName,&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[UIFont&fontWithName:@"HelveticaNeue-CondensedBlack"&size:21.0],&NSFontAttributeName,&nil]];&
运行效果如下图所示:
修改导航栏标题为图片
如果要想将导航栏标题修改为一个图片或者logo,那么只需要使用下面这行代码即可:
self.navigationItem.titleView&=&[[UIImageView&alloc]&initWithImage:[UIImage&imageNamed:@"appcoda-logo.png"]];&
上面的代码简单的修改了titleView属性,将一个图片赋值给它。 注意:这不是iOS 7中的新功能,之前的iOS版本就可以已经有了。具体效果如下图所示:
添加多个按钮
同样,这个技巧也不是iOS 7的,开发者经常会在导航栏中添加多个按钮,所以我决定在这里进行介绍。我们可以在导航栏左边或者右边添加多个按钮。例如,我们希望在导航栏右边添加一个照相机和分享按钮,那只需要使用下面的代码即可:
UIBarButtonItem&*shareItem&=&[[UIBarButtonItem&alloc]&initWithBarButtonSystemItem:UIBarButtonSystemItemAction&target:self&action:nil];&
UIBarButtonItem&*cameraItem&=&[[UIBarButtonItem&alloc]&initWithBarButtonSystemItem:UIBarButtonSystemItemCamera&target:self&action:nil];&
NSArray&*actionButtonItems&=&@[shareItem,&cameraItem];&
self.navigationItem.rightBarButtonItems&=&actionButtonI&
如下效果:
修改状态栏的风格
在老版本的iOS中,状态栏永远都是白色风格。而在iOS 7中,我们可以修改每个view controller中状态栏的外观。通过UIStatusBarStyle常量可以指定状态栏的内容是暗色或亮色。默认情况下,状态栏的显示是暗色。也就是说,状态栏上的时间、电池指示器和Wi-Fi信号显示为暗色。如果导航栏中使用暗色为背景,那么看起来的效果如下图所示:
如上图这种情况下,我们可能希望将导航栏的风格修改为亮色。这里有两个方法可以实现。在iOS 7中,我们可以在每个view controller中overridingpreferredStatusBarStyle:方法,如下所示:
-(UIStatusBarStyle)preferredStatusBarStyle&
&&&&return&UIStatusBarStyleLightC&
上面代码的效果如下图所示:
在iOS 7中,通过上面的方法来修改状态栏风格非常的棒。另外,我们也可以使用UIApplication的statusBarStyle方法来设置状态栏,不过,首先需要停止使用View controller-based status bar appearance。在project target的Info tab中,插入一个新的key,名字为View controller-based status bar appearance,并将其值设置为NO。
然后就可以使用下面的代码来设置状态栏风格了:
[[UIApplication&sharedApplication]&setStatusBarStyle:UIStatusBarStyleLightContent];&
隐藏状态栏
有时候我们需要隐藏状态栏,那么此时我们在view controller中override方法prefersStatusBarHidden:即可,如下代码所示:
-&(BOOL)prefersStatusBarHidden&
&&&&return&YES;&
iOS 7给开发者提供了一些新的自由度来定制导航栏和状态栏的外观。希望上面的这些技巧能对你有用。这里可以下载到。只需要取消相关代码注释即可进行测试。

我要回帖

更多关于 ios 导航栏偏移20 的文章

 

随机推荐