文章目录

    • iOS 15 之后导航栏背景色的设置
    • 1、状态栏设置
      • 1.1、没有导航栏
      • 1.2、有导航栏
    • 2、导航栏背景和字体颜色
      • 2.1、十六进制颜色转RGB
      • 2.2、生成纯色图片
    • 3、导航栏的另外一种设置方式
    • 4、导航栏右滑返回失效
    • 5、导航栏的一些设置会影响导航栏是否遮挡view
    • 6、总结导航栏和View布局问题
      • 6.1、view在导航栏下开始布局
      • 6.2、view从(0,0)开始布局,导航栏遮挡view
  • 更新
    • 7、关于 View 从导航栏顶部布局(被导航栏遮挡)和从导航栏底部布局的新的理解。
      • 7.1属性 translucent 的官方介绍
    • 8、View布局位置
    • 9、ScrollView的布局影响
    • 10、translucent=NO 的时候设置导航栏背景色透明

iOS 15 之后导航栏背景色的设置

iOS 13 开始新增了 standardAppearance 和 scrollEdgeAppearance 属性,不过在iOS 15(xcode13)的时候才真正需要适配。带滑动视图的页面,当滑动到最顶部时显示后者的属性,其他时候显示前者的属性。不带滑动视图的页面只显示前者的属性。

if (@available(iOS 15.0, *)) {UINavigationBarAppearance *barAppearance = [[UINavigationBarAppearance alloc] init];barAppearance.backgroundImage = backgroundImage;barAppearance.shadowImage = shadowImage;self.navigationController.navigationBar.standardAppearance = barAppearance;self.navigationController.navigationBar.scrollEdgeAppearance = barAppearance;}

1、状态栏设置

typedef NS_ENUM(NSInteger, UIStatusBarStyle) {UIStatusBarStyleDefault                                  = 0, // Automatically chooses light or dark content based on the user interface styleUIStatusBarStyleLightContent     API_AVAILABLE(ios(7.0)) = 1, // Light content, for use on dark backgroundsUIStatusBarStyleDarkContent     API_AVAILABLE(ios(13.0)) = 3, // Dark content, for use on light backgroundsUIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,UIStatusBarStyleBlackOpaque      NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,
} API_UNAVAILABLE(tvos);

UIStatusBarStyleDefault // 默认状态
UIStatusBarStyleLightContent // 状态栏文本和图标为白色
UIStatusBarStyleDarkContent // 状态栏文本和图标为黑色
另外两个已弃用

1.1、没有导航栏

在 ViewController 中,使用 -(UIStatusBarStyle)preferredStatusBarStyle 方法设置

// ViewController
- (UIStatusBarStyle)preferredStatusBarStyle
{return UIStatusBarStyleLightContent;
}

1.2、有导航栏

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
typedef NS_ENUM(NSInteger, UIBarStyle) {UIBarStyleDefault          = 0,UIBarStyleBlack            = 1,UIBarStyleBlackOpaque API_DEPRECATED("Use UIBarStyleBlack instead.", ios(2.0, 13.0)) = 1,UIBarStyleBlackTranslucent API_DEPRECATED("Use UIBarStyleBlack and set the translucent property to YES instead.", ios(2.0, 13.0)) = 2,
} API_UNAVAILABLE(tvos);

UIBarStyleDefault // 状态栏文本和图标为黑色
UIBarStyleBlack // 状态栏文本和图标为白色
另外两个已弃用

2、导航栏背景和字体颜色

 // 状态栏深色背景浅色字体(状态栏字体白色)[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];// 导航栏背景色// self.navigationController.navigationBar.barTintColor = [UIColor blueColor];// 导航栏背景图片[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff3d3b)] forBarMetrics:UIBarMetricsDefault];// 导航栏标题颜色[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];// 导航栏按钮颜色[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

2.1、十六进制颜色转RGB

 //3.获得RGB颜色#define UIColorFromRGBAlpha(rgbValue, rgbAlpha) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:rgbAlpha]#define UIColorFromRGB(rgbValue) UIColorFromRGBAlpha(rgbValue, 1.0f)

2.2、生成纯色图片

+(UIImage *)imageWithColor:(UIColor *)aColor
{return [self imageWithColor:aColor withFrame:CGRectMake(0, 0, 1, 1)];
}+(UIImage *)imageWithColor:(UIColor *)aColor withFrame:(CGRect)aFrame
{UIGraphicsBeginImageContext(aFrame.size);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [aColor CGColor]);CGContextFillRect(context, aFrame);UIImage *img = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return img;
}

3、导航栏的另外一种设置方式

[UINavigationBar appearance] 获取的是当前已经展示的导航栏,在调用系统通讯录的时候(CNContactPickerViewController)使用这种方式修改通讯录导航栏样式

 UINavigationBar *navigationBar = [UINavigationBar appearance];// 状态栏深色背景浅色字体(状态栏字体白色)[navigationBar setBarStyle:UIBarStyleBlack];// 导航栏背景色[navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff3d3b)] forBarMetrics:UIBarMetricsDefault];// 导航栏标题颜色[navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];// 导航栏按钮颜色[navigationBar setTintColor:[UIColor whiteColor]];

4、导航栏右滑返回失效

导航栏设置了左侧按钮(self.navigationItem.leftBarButtonItem)右滑返回失效

 // 在interface声明代理 <UIGestureRecognizerDelegate>// 自定义左侧按钮后右滑返回失效,如下代码恢复右滑返回功能self.navigationController.interactivePopGestureRecognizer.delegate = self;

5、导航栏的一些设置会影响导航栏是否遮挡view

 //设置导航栏透明,导航栏透明度默认为true,设置为No,view布局从导航栏底部开始[self.navigationController.navigationBar setTranslucent:true];//把背景设为空,image设为nil的话会有一个半透明黑色图层,设为一个没有内容的图片,导航栏就是透明的了,view的布局从0,0开始,会被遮挡。[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

image为nil的效果

导航栏遮住View问题

6、总结导航栏和View布局问题

(这里我对translucent属性的理解有些问题,文底做说明)

6.1、view在导航栏下开始布局

(1)导航栏不透明,navigationBar.translucent 设为YES(默认为YES),设置背景图片(不为nil,且有内容);

//    self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];// 这里其实 self.navigationController.navigationBar.translucent = NO;// 文底会做说明

(2) 导航栏不透明,navigationBar.translucent 设为NO,导航栏的背景图片颜色为黑色;

 self.navigationController.navigationBar.translucent = NO;

(3)导航栏透明,navigationBar.translucent 设为YES(默认为YES),edgesForExtendedLayout 设为 UIRectEdgeNone。

// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];self.edgesForExtendedLayout = UIRectEdgeNone;

6.2、view从(0,0)开始布局,导航栏遮挡view

(1)导航栏不透明,上述情况下设置extendedLayoutIncludesOpaqueBars属性为YES

//    self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];self.extendedLayoutIncludesOpaqueBars = YES;

(2) 导航栏不透明,navigationBar.translucent 设为NO,导航栏的背景图片颜色为黑色;

 self.navigationController.navigationBar.translucent = NO;self.extendedLayoutIncludesOpaqueBars = YES;

(3)导航栏透明

 //    self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

更新

--------------------------------------------------------------------更新于21.03.16--------------------------------------------------------------------

7、关于 View 从导航栏顶部布局(被导航栏遮挡)和从导航栏底部布局的新的理解。

本来打算修改上面的文章,感觉自己走过的弯路保留一下吧,从这里开始。

7.1属性 translucent 的官方介绍

self.navigationController.navigationBar.translucent

下面是文档说明

When the navigation bar is translucent, configure the edgesForExtendedLayout and extendedLayoutIncludesOpaqueBars properties of your view controller to display your content underneath the navigation bar.
If the navigation bar doesn't have a custom background image, or if any pixel of the background image has an alpha value of less than 1.0, the default value of this property is YES. If the background image is completely opaque, the default value of this property is NO. If you set this property to YES and the custom background image is completely opaque, UIKit applies a system-defined opacity of less than 1.0 to the image. If you set this property to NO and the background image is not opaque, UIKit adds an opaque backdrop.

第一段是说导航栏为半透明的(translucent=YES),可以通过edgesForExtendedLayout 和 extendedLayoutIncludesOpaqueBars两个属性来控制view从导航栏的顶部还是底部开始布局。(这个后面再说,先理解第二段)

第二段是说明导航栏透明度(translucent)和背景图片(backgroundImage)的关系。

translucent 默认值为 YES不指定translucent的值,背景图为不透明的,translucent自动为NO;
背景图为透明的,translucent自动为yes;
背景图不透明,指定translucent为YES,则背景图自动变为半透明的;
背景图透明,指定translucent为NO,则背景图变为不透明的;

或者说,不指定或者先指定translucent的值,不管是YES还是NO,再设定背景图,translucent 的值以图片的透明度为准;
先指定背景图,再指定translucent,translucent的值以指定的值为准。

8、View布局位置

// View 从导航栏顶部开始布局,会被导航栏遮挡;
translucent=YES,
// View 从导航栏底部开始布局。
translucent=NO ,// View 从导航栏底部开始布局。
translucent=YES 且
self.edgesForExtendedLayout = UIRectEdgeNone;
// View 从导航栏顶部开始布局,会被导航栏遮挡;
translucent=NO  且
self.extendedLayoutIncludesOpaqueBars = YES;

9、ScrollView的布局影响

偷个懒,看这里吧。
导航栏遮住View问题

没有做处理的话,无论导航栏透明不透明都不会遮挡。

10、translucent=NO 的时候设置导航栏背景色透明

这里记录一个设置导航栏透明的方法,这个改变的是view的透明度,不影响导航栏背景图片,所以也不会修改 translucent 的值,如果有滑动控制导航栏透明度的需求可以设置这里。

[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];

iOS设置导航栏和状态栏相关推荐

  1. IOS设置导航栏返回按钮,并添加事件返回主页面

    IOS设置导航栏返回按钮,并添加事件返回主页面 前提是已经push了一个viewController了.才能使用. XXXTableViewController 里面书写 - (void)viewDi ...

  2. IOS设置导航栏的背景图片和文字

    IOS设置导航栏的背景图片和文字 - (void)viewDidLoad {[super viewDidLoad];[self.navigationBar setBackgroundImage:[UI ...

  3. 控制iOS的导航栏和状态栏的样式

    这是一个很常用的开发场景,就是改变导航栏上的文字颜色与背景色,如果你曾有 windows form 开发经验一定会笑我:"卧槽,这有什么好写的,不就是设置两个属性就可以了吗?" 我 ...

  4. iOS 设置导航栏背景颜色

    //导航栏背景色UIColor *navBarColor = [UIColor colorWithRGB_au:0xD42D37];if (@available(iOS 15.0, *)) {UINa ...

  5. iOS - 设置导航栏之标题栏居中、标题栏的背景颜色

    本章实现效果: Untitled.gif 前言: 项目中很多需求是要求自定义标题栏居中的,本人最近就遇到这中需求,如果用系统自带的titleView设置的话,不会居中,经过尝试,发现titleview ...

  6. 定制iOS 7中的导航栏和状态栏

    近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这也就意味着导航栏 ...

  7. IOS 11 适配导航栏、状态栏调整

    在ios中对于导航栏和状态栏调整遇到问题的,请往下看. 如果系统添加导航栏和状态栏满足不了需求,可以尝试自定义制作满足自己需要的,以下提供方法和属性,让你自定义制作. 注意:状态栏  20像素     ...

  8. ios隐藏导航栏底线条和导航、状态栏浙变色

    方法一遍历法: 在你需要隐藏的地方调用如下代码 [self findlineviw:self.navigationBar].hidden = YES; -(UIImageView*)findlinev ...

  9. 微信h5隐藏导航栏和状态栏_导航栏未在SwiftUI中隐藏

    微信h5隐藏导航栏和状态栏 TL;DR: If you can't tap on buttons in the navigation-bar area, you can jump directly t ...

最新文章

  1. redis(一) 安装以及基本数据类型操作
  2. 计算机网络工程实验分析与体会_《软件工程》面向对象分析实验
  3. mysql自增id前端安全显示_mysql使用自增id遇到的坑
  4. AtCoder Beginner Contest 230
  5. LeetCode 873. 最长的斐波那契子序列的长度(动态规划)
  6. Android搭建web,Android手机搭建WEB环境
  7. Android应用开发(10)---资源类型
  8. 2021-2025年中国抽屉加热器行业市场供需与战略研究报告
  9. linux python3 装pip,linux 安装pip 和python3(示例代码)
  10. 界面开发用qt还是java,做windows界面,用QT还是MFC?
  11. java对接金蝶接口
  12. 全网最全诊断梳理《UDS/OBD诊断诊断描述文件CDD》总目录
  13. 【分享实录-猫眼电影】业务纵横捭阖背后的技术拆分与融合
  14. BZOJ 1213 HNOI2004 高精度开根 二分+高(Py)精(thon)度
  15. 全面设防 让广播风暴远离局域网
  16. 惠普服务器硬盘指示灯不亮或显示蓝色
  17. c语言输出行末无空格_三个简单的C语言程序
  18. 崩坏3桌面版怎么更换服务器,崩坏3桌面版安装步骤介绍_崩坏3桌面版安装指南_玩游戏网...
  19. 【OSX】MAC下能用的炒股软件
  20. 非完备信息的机器博弈-麻将篇

热门文章

  1. 【android】音乐播放器之UI设计的点点滴滴
  2. 弹性系数系数在水文气象中的应用及其MATLAB实现
  3. 踩坑 Mac Xcode 与 conio.h windows.h 库(如何解决错误代码:‘conio.h‘ file not found ‘windows.h‘ file not found)
  4. 2017年软件测试就业前景趋势
  5. 用limma对芯片数据做差异分析
  6. Excel操作:使用offset函数让数据透视表动态更新
  7. 用Python一键生成微信好友头像墙
  8. 视频版宠物自动喂食器方案
  9. Python语言Flask开发框架实现个性化新闻推荐网 在线新闻推荐系统 基于用户、物品的协同过滤推荐算法开发
  10. Halcon学习笔记之曲面矫正系列(棋盘格标定)-曲面码如何读取