iOS 7+自定义导航栏背景,标题和返回按钮文字颜色
2015-07-29 16:43
756 查看
在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem
方法一:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen]bounds]];
ViewController *mainView = [[ViewController
alloc]init];
UINavigationController *navigation = [[UINavigationController
alloc]
initWithRootViewController:mainView];
mainView.title =
@"首页";
[self.window
setRootViewController:navigation];
// set NavigationBar
背景颜色&title 颜色
[navigation.navigationBar
setBarTintColor:[UIColor
colorWithRed:20/255.0
green:155/255.0
blue:213/255.0
alpha:1.0]];
[navigation.navigationBar
setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor
whiteColor],NSForegroundColorAttributeName,nil]];
[self.window
makeKeyAndVisible];
return
YES;
}
效果如下:
我们把背景改成了蓝色,title文字改成了白色,是不是很简单呢?NavigationBar及其push过去的子页面也会是你修改后的背景颜色
方法二:
// 设置NavigationBar背景颜色
[[UINavigationBar
appearance] setBarTintColor:[UIColor
redColor]];
// @{}代表Dictionary
[[UINavigationBar
appearance] setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor
blueColor]}];
效果如下:
如果您的应用程序使用了自定义图像作为栏的背景,你需要提供一个“更大”的图片,使其延伸了状态栏的后面。导航栏的高度现在是从44点(88像素)更改为64点(128像素)。
仍然可以使用了setBackgroundImage:方法来指定自定义图像的导航栏。下面是代码行设置背景图片:
[[UINavigationBar
appearance] setBackgroundImage:[UIImage
imageNamed:@"navigationBarBackground"]
forBarMetrics:UIBarMetricsDefault];
效果图和上面的一样,我就不贴出来了。
就像iOS 6,我们可以通过使用导航栏的“titleTextAttributes”属性来自定义的文本样式。可以指定字体,文字颜色,文字阴影颜色,文字阴影在文本标题偏移属性字典,使用下面的文本属性键:
UITextAttributeFont - 字体
UITextAttributeTextColor - 文字颜色
UITextAttributeTextShadowColor - 文字阴影颜色
UITextAttributeTextShadowOffset - 偏移用于文本阴影
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:30.0],
NSFontAttributeName,
nil]];
不想标题栏是光秃秃的文字?可以通过使用代码行中的图像或标志在view.m文件里取代它:简单地改变titleview用来自定义(适用于较低版本)
self.navigationItem.titleView = [[UIImageView
alloc] initWithImage:[UIImage
imageNamed:@"home-selected"]];
效果如下,我随便用了个图片,别介意:
您希望添加导航栏的一侧不止一个栏按钮项目,无论是leftBarButtonItems和rightBarButtonItems 您在导航栏左侧/右侧指定自定义栏按钮项目。比如你想添加一个摄像头和一个共享按钮右侧的吧。您可以使用下面的代码:
// 创建系统自带按钮
UIBarButtonItem *shareItem = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action: nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self
action: nil];
//
创建自定义带标题按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem
alloc] initWithTitle:@"下一页"
style:UIBarButtonItemStylePlain
target:self
action:@selector(callModalList)];
//
自定义按钮标题颜色
rightButton.tintColor=[UIColor
colorWithRed:74/255.0
green:74/255.0
blue:74/255.0
alpha:1.0];
//
创建自定义带图片按钮
//UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting-selected"] style:UIBarButtonItemStylePlain target:self action:nil];
//
添加多个按钮
NSArray *itemsArr =
@[rightButton,shareItem,cameraItem];
self.navigationItem.rightBarButtonItems = itemsArr;
//
添加一个按钮
//self.navigationItem.rightBarButtonItem = rightButton;
通常情况下,我们使用UINavigationController时,push到的子页面,左上角会是系统自动取值上一层父页面的title名称,默认情况是这样,那么我们该如何修改它呢?
左侧显示了父页面的title:用户登录,可是我们想修改成返回,方式有很多,举些例子
方法一:
// 通过设置navigationItem的backBarButtonItem可以直接更换文字,【注意,要在父视图的Controller中设置】如下:
UIBarButtonItem *item = [[UIBarButtonItem
alloc] initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem = item;
效果如下:
所有的子界面返回时都变成了我们定义的文字,如果不想显示文字,直接"",就会单独显示一个系统的返回箭头图标,也是很清晰的感觉。做到这里发现文字颜色和背景有重复,那么如何自定义其颜色呢?在iOS7,可以改变tintColor属性,它提供了一个快速和简单的方式,下面是一个示例代码片段:
[[UINavigationBar
appearance] setTintColor:[UIColor
whiteColor]];
效果如下:
全是系统的图标和文字,这回看着舒服了,有木有?【除了后退按钮,请注意,tintColor属性影响所有按钮标题和按钮图像】
最后举个例子,另外一种实现自定义导航控制器返回按钮,代码如下:
// 自定义返回按钮
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor
redColor],NSFontAttributeName:[UIFont
systemFontOfSize:19.0]}];
self.title = [NSString
stringWithFormat:@"第%lu页",(unsigned
long)self.navigationController.viewControllers.count];
UIImage *backButtonImage = [[UIImage
imageNamed:@"bluetooth-on"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,
30,
0, 0)];
//
将返回按钮的文字position设置不在屏幕上显示
[[UIBarButtonItem
appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem
appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin,
NSIntegerMin)
forBarMetrics:UIBarMetricsDefault];
效果如下:
最后说一下使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮(本段摘自网络):
1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;
2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;
3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题;
导航条跳转页面的考虑
对于用navigationcontroller来跳转页面的时候,其实是执行堆栈的进栈和出栈的操作,要想释放内存,那么在来回跳转的时候,就要考虑几个问题了
1:A=>B=>C=>D,
D=>A
有根视图的话
(HOME)
[self.navigationController popToRootViewControllerAnimated:YES];
D=>C (每一个界面返回上一层)
[self.navigationController popViewControllerAnimated:YES];
返回到上一层,并且传递参数
CViewController *cvc = [CViewController alloc]init];
cvc.str =
self.str;
[self.navigationController popToViewController:cvc animated:true];
返回到上一层后,上一页面显示后要接收参数,并刷新。注意此时应该在viewDidAppear中进行判断并接收传递的值
-(void)viewDidAppear:(BOOL)animated
{
//判断并接收返回的参数
}
2:A =>B=>C=>D=>E,E=>B=>C=>E
因为B在之前已经出现过,不能在E中直接PUSH到B,因为那样已经是两个B了,增加内存,所以在跳转的时候,就要进行判断是否之前已经出现过B了,出现过,则直接push。这样push到的是原有的B,不会在内存中重新生成一个B了。
NSArray *array =
self.navigationController.viewControllers;
for (UIViewController *vc
in array) {
if ([vc isKindOfClass:[BXXXViewController class]]) {
push VC;
}
}
或者知道每个界面的指针
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:
([self.navigationController.viewControllers count] -4)] animated:YES];
在使用时,根据自己返回层的需要,只要改变一下“-4”这个数字就可以达到目的了
更改导航栏的背景和文字Color
方法一:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen]bounds]];
ViewController *mainView = [[ViewController
alloc]init];
UINavigationController *navigation = [[UINavigationController
alloc]
initWithRootViewController:mainView];
mainView.title =
@"首页";
[self.window
setRootViewController:navigation];
// set NavigationBar
背景颜色&title 颜色
[navigation.navigationBar
setBarTintColor:[UIColor
colorWithRed:20/255.0
green:155/255.0
blue:213/255.0
alpha:1.0]];
[navigation.navigationBar
setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor
whiteColor],NSForegroundColorAttributeName,nil]];
[self.window
makeKeyAndVisible];
return
YES;
}
效果如下:
我们把背景改成了蓝色,title文字改成了白色,是不是很简单呢?NavigationBar及其push过去的子页面也会是你修改后的背景颜色
方法二:
// 设置NavigationBar背景颜色
[[UINavigationBar
appearance] setBarTintColor:[UIColor
redColor]];
// @{}代表Dictionary
[[UINavigationBar
appearance] setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor
blueColor]}];
效果如下:
在导航栏使用背景图片:
如果您的应用程序使用了自定义图像作为栏的背景,你需要提供一个“更大”的图片,使其延伸了状态栏的后面。导航栏的高度现在是从44点(88像素)更改为64点(128像素)。仍然可以使用了setBackgroundImage:方法来指定自定义图像的导航栏。下面是代码行设置背景图片:
[[UINavigationBar
appearance] setBackgroundImage:[UIImage
imageNamed:@"navigationBarBackground"]
forBarMetrics:UIBarMetricsDefault];
效果图和上面的一样,我就不贴出来了。
改变导航栏标题的字体
就像iOS 6,我们可以通过使用导航栏的“titleTextAttributes”属性来自定义的文本样式。可以指定字体,文字颜色,文字阴影颜色,文字阴影在文本标题偏移属性字典,使用下面的文本属性键:UITextAttributeFont - 字体
UITextAttributeTextColor - 文字颜色
UITextAttributeTextShadowColor - 文字阴影颜色
UITextAttributeTextShadowOffset - 偏移用于文本阴影
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:30.0],
NSFontAttributeName,
nil]];
使用图片作为导航栏标题
不想标题栏是光秃秃的文字?可以通过使用代码行中的图像或标志在view.m文件里取代它:简单地改变titleview用来自定义(适用于较低版本)self.navigationItem.titleView = [[UIImageView
alloc] initWithImage:[UIImage
imageNamed:@"home-selected"]];
效果如下,我随便用了个图片,别介意:
添加多个栏按钮项目
您希望添加导航栏的一侧不止一个栏按钮项目,无论是leftBarButtonItems和rightBarButtonItems 您在导航栏左侧/右侧指定自定义栏按钮项目。比如你想添加一个摄像头和一个共享按钮右侧的吧。您可以使用下面的代码:// 创建系统自带按钮
UIBarButtonItem *shareItem = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action: nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self
action: nil];
//
创建自定义带标题按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem
alloc] initWithTitle:@"下一页"
style:UIBarButtonItemStylePlain
target:self
action:@selector(callModalList)];
//
自定义按钮标题颜色
rightButton.tintColor=[UIColor
colorWithRed:74/255.0
green:74/255.0
blue:74/255.0
alpha:1.0];
//
创建自定义带图片按钮
//UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting-selected"] style:UIBarButtonItemStylePlain target:self action:nil];
//
添加多个按钮
NSArray *itemsArr =
@[rightButton,shareItem,cameraItem];
self.navigationItem.rightBarButtonItems = itemsArr;
//
添加一个按钮
//self.navigationItem.rightBarButtonItem = rightButton;
自定义后退按钮的文字和颜色
通常情况下,我们使用UINavigationController时,push到的子页面,左上角会是系统自动取值上一层父页面的title名称,默认情况是这样,那么我们该如何修改它呢?左侧显示了父页面的title:用户登录,可是我们想修改成返回,方式有很多,举些例子
方法一:
// 通过设置navigationItem的backBarButtonItem可以直接更换文字,【注意,要在父视图的Controller中设置】如下:
UIBarButtonItem *item = [[UIBarButtonItem
alloc] initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem = item;
效果如下:
所有的子界面返回时都变成了我们定义的文字,如果不想显示文字,直接"",就会单独显示一个系统的返回箭头图标,也是很清晰的感觉。做到这里发现文字颜色和背景有重复,那么如何自定义其颜色呢?在iOS7,可以改变tintColor属性,它提供了一个快速和简单的方式,下面是一个示例代码片段:
[[UINavigationBar
appearance] setTintColor:[UIColor
whiteColor]];
效果如下:
全是系统的图标和文字,这回看着舒服了,有木有?【除了后退按钮,请注意,tintColor属性影响所有按钮标题和按钮图像】
最后举个例子,另外一种实现自定义导航控制器返回按钮,代码如下:
// 自定义返回按钮
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor
redColor],NSFontAttributeName:[UIFont
systemFontOfSize:19.0]}];
self.title = [NSString
stringWithFormat:@"第%lu页",(unsigned
long)self.navigationController.viewControllers.count];
UIImage *backButtonImage = [[UIImage
imageNamed:@"bluetooth-on"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,
30,
0, 0)];
//
将返回按钮的文字position设置不在屏幕上显示
[[UIBarButtonItem
appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem
appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin,
NSIntegerMin)
forBarMetrics:UIBarMetricsDefault];
效果如下:
最后说一下使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮(本段摘自网络):
1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;
2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;
3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题;
导航条跳转页面的考虑
对于用navigationcontroller来跳转页面的时候,其实是执行堆栈的进栈和出栈的操作,要想释放内存,那么在来回跳转的时候,就要考虑几个问题了
1:A=>B=>C=>D,
D=>A
有根视图的话
(HOME)
[self.navigationController popToRootViewControllerAnimated:YES];
D=>C (每一个界面返回上一层)
[self.navigationController popViewControllerAnimated:YES];
返回到上一层,并且传递参数
CViewController *cvc = [CViewController alloc]init];
cvc.str =
self.str;
[self.navigationController popToViewController:cvc animated:true];
返回到上一层后,上一页面显示后要接收参数,并刷新。注意此时应该在viewDidAppear中进行判断并接收传递的值
-(void)viewDidAppear:(BOOL)animated
{
//判断并接收返回的参数
}
2:A =>B=>C=>D=>E,E=>B=>C=>E
因为B在之前已经出现过,不能在E中直接PUSH到B,因为那样已经是两个B了,增加内存,所以在跳转的时候,就要进行判断是否之前已经出现过B了,出现过,则直接push。这样push到的是原有的B,不会在内存中重新生成一个B了。
NSArray *array =
self.navigationController.viewControllers;
for (UIViewController *vc
in array) {
if ([vc isKindOfClass:[BXXXViewController class]]) {
push VC;
}
}
或者知道每个界面的指针
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:
([self.navigationController.viewControllers count] -4)] animated:YES];
在使用时,根据自己返回层的需要,只要改变一下“-4”这个数字就可以达到目的了
相关文章推荐
- ios开发多个mac共用证书(ios开发证书移植)
- 小白学开发(iOS)OC_类的互引用(2015-07-29)
- 小白学开发(iOS)OC_new方法原理(2015-07-29)
- 小白学开发(iOS)OC_构造方法(2015-07-29)
- 小白学开发(iOS)OC_私有方法(2015-07-28)
- 小白学开发(iOS)OC_ id类型(2015-07-28)
- 小白学开发(iOS)OC_get/set 方法重写(2015-07-28)
- 苹果开发 笔记(54)ZipArchive
- 小白学开发(iOS)OC_property和synthesize(2015-07-28)
- IOS开发-16.自定义不等高cell
- ios 常用的正则表达式
- iOS开发-Day12-C的复习
- ios 解决键盘挡住输入框的问题
- 猫猫学iOS 之微博项目实战(5)微博自定义搜索框searchBar
- iOS培训哪家好?我选小码哥iOS开发培训课程
- ios 使用GCD控制 发送短信 按钮时间间隔
- IOS Sent Events 事件
- iOS-transform属性
- iOS开发多线程篇—GCD的常见用法
- ios coreData 根据应用版本升级进行更新