菜鸟学习UINavigation

UINavigation Bar For IOS7

IOS7中将UINavigation Bar和Status Bar连接在一起,给人呈现一种融为一体的感觉,而不再是独立的一部分。

UInavigation Bar的几个属性在上图我们可以清晰看到,通常我们只需要操作这几个属性,还有leftBarButtonItems,rightBarButtonItems,下面是我们使用UINavigation Bar过程中会经常用到的方法:

设置背景颜色

1
[[UINavigationBar appearance] setBarTintColor:color]]

如果是使用默认的UINavigationController,则在

1
2
3
4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]];//设置自己的颜色
}

而在自定义的UINavigationController中,则

1
2
3
4
- (void)viewDidLoad
{
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]];//设置自己的颜色
}

值得注意的是:如果在子Controller的viewDidLoad中加入此代码时,则会发生切换回其它viewController时,UINavigation Bar背景会改变为子viewController改变的颜色,而当前的viewConroller则不会改变。

设置背景图片

1
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

设置返回的图片

1
2
[[UINavigationBar appearance] setBackIndicatorImage:image];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];

但需要注意的是,这两个是缺一不可的。

设置返回字体颜色

1
[[UINavigationBar appearance] setTintColor:color];

自定义标题

1
2
3
4
[[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,[UIFont fontWithName:@"HelveticaNeue-Light" size:22.0],
NSFontAttributeName, nil]];

添加按钮

在viewController的viewDidLoad中加,注:下面代码是加单个右按钮,多个则使用rightBarButtonItems,而左按钮则是leftBarButtonItem和leftBarButtonItems。

1
2
3
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:nil];

self.navigationItem.rightBarButtonItem = doneItem;