zoukankan      html  css  js  c++  java
  • 自定义iOS导航栏背景,标题和返回按钮文字颜色

    一、导航栏的背景和文字Color:

    方法一:

    1. //设置NavigationBar 背景颜色&title 颜色  
    2. [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];  
    3. [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];  

    方法二:

    1. //设置NavigationBar背景颜色  
    2. [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];  
    3. //@{}代表Dictionary  
    4. [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];  

    二、导航栏使用背景图片:

    1. [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];  

    三、导航栏标题的字体:

    UITextAttributeFont - 字体

    UITextAttributeTextColor - 文字颜色

    UITextAttributeTextShadowColor - 文字阴影颜色

    UITextAttributeTextShadowOffset - 偏移用于文本阴影

    1. NSShadow *shadow = [[NSShadow alloc] init];  
    2. shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];  
    3. shadow.shadowOffset = CGSizeMake(0, 1);  
    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,  
    5. shadow, NSShadowAttributeName,  
    6. [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil nil]];  

    四、图片作为导航栏标题:

    1. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];  

    五、添加多个栏按钮项目:

    1. UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action: nil nil];  
    2. UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action: nil nil];  
    3. NSArray *itemsArr = @[shareItem,cameraItem];  
    4. self.navigationItem.rightBarButtonItems = itemsArr; 

    六、自定义后退按钮的文字和颜色:

    方法一:

    1. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];  
    2. self.navigationItem.backBarButtonItem = item;  
    1. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];  
    1. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];  
    2.   
    3. self.title=[NSString stringWithFormat:@"第%lu页",(unsigned long)self.navigationController.viewControllers.count];  
    4.   
    5. //自定义返回按钮  
    6. UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];  
    7. [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];  
    8. //将返回按钮的文字position设置不在屏幕上显示  
    9. [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];  
  • 相关阅读:
    datagrid在MVC中的运用05-加入时间搜索条件,枚举填充下拉框
    datagrid在MVC中的运用04-同时添加搜索和操作区域
    datagrid在MVC中的运用03-选择单行或多行
    datagrid在MVC中的运用02-结合搜索
    datagrid在MVC中的运用01-基本属性并实现分页
    Object [object Object] has no method 'live'
    AutoMapper在MVC中的运用小结
    error CS0234: 命名空间“XXX”中不存在类型或命名空间名称“UserInfoVm”(是否缺少程序集引用?)
    《黄聪:手机移动站SEO优化教程》2、PC端和手机移动端SEO优化区别
    《黄聪:手机移动站SEO优化教程》1、为什么要做手机移动端网站
  • 原文地址:https://www.cnblogs.com/yuhao309/p/6698648.html
Copyright © 2011-2022 走看看