1、导航错位问题
iPhone的状态栏可以设置成半透明, 这是众所周知的, 用UINavigationController,在页面切换时, 如果你的子viewController的view是全屏的(480*320) 就会出现错位, 表现是所以的子view会向下偏移了 20像素 (就是状态栏的高度).
我搜索了好长时间也没有找到答案, 于是自己去看官方文档, 结果发现了这个方法, 这是UIViewController的一个属性.
wantsFullScreenLayout
看字面意思就很好理解(苹果的方法名都很好理解, 还是带时态和语法的 很人性化), 就是”我要全屏布局” 这样错位的问题就解决了
2、导航器中添加标题
在开发中经常会用到导航器,在导航器中添加标题很简单,如下:
<A href="mailto:self.title=@"Elimination">self.title=@"Elimination phase Day 1";
但是如果题目太长,后半部分就变成省略号了,那要实现自定义字体,代码和效果如下:
UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 20)];
titleText.backgroundColor = [UIColor clearColor];
[titleText setFont:[UIFont systemFontOfSize:15.0]];
[titleText setText:@"Elimination phase Day 1"];
self.navigationItem.titleView=titleText;
[titleText release];
3、导航栏中添加各种样式的按钮
//添加一个按钮
在实际的开发中,导航器是最重要的容器之一,我们经常要在导航栏中添加各种样式的按钮,添加一个按钮很简单,代码如下图:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UITabBarSystemItemContacts
target:self action:@selector(clickSettings:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
其中按钮的样式可以有多种,具体的可以参考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/
在有些项目中要在右面添加两个按钮,
//添加两个按钮
实现的代码如下图:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];
[tools setTintColor:[self.navigationController.navigationBar tintColor]];
[tools setAlpha:[self.navigationController.navigationBar alpha]];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickSettings:)];
UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContacts target:self action:@selector(clickEdit:)];
[buttons addObject:anotherButton];
[anotherButton release];
[buttons addObject:anotherButton1];
[anotherButton1 release];
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = myBtn;
[myBtn release];
[tools release];
导航控制器支持向下扩展视图,在各视图间平滑地切换显示。导航控制器会记住视图显示的顺序,并提供完整的“返回”按钮痕迹导航,因此不需要额外编程就可返回之前的视图。