导航条、状态栏的设置

感觉这种UI的东西苹果底层经常会变,可能系统过几个版本又不同了(比如kvc设值的方式),无力吐槽真的,还是记录一下

关于状态栏隐藏和显示问题

1、全局隐藏或者修改状态栏的文字颜色

在info.plist 文件:将View controller-based status bar appearance 的值设置为NO

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

2、单独界面设置
在info.plist 文件:将View controller-based status bar appearance 的值设置为YES

- (UIStatusBarStyle)preferredStatusBarStyle{

    return self.topViewController.preferredStatusBarStyle;
}

warn:如果viewcontroller是嵌套在导航控制器之下,需在导航控制器中设置

3、自定义背景颜色

下面的方法iOS 13 会崩溃

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor cyanColor];
}

取代方法是直接加一层

CGFloat stateBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIView *customBState = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, stateBarHeight)];
customBState.backgroundColor = [UIColor greenColor];
[self.view addSubview:customBState];
[self.view bringSubviewToFront:customBState];

如果在嵌套有导航控制器的情况下,请在viewcontroller对应的导航控制器中添加这一层

导航栏

self.navigationBar.barTintColor = [UIColor purpleColor];//背景色
[self.navigationBar setBackgroundImage:[self createImageWithColor:UIColor.redColor] forBarMetrics:UIBarMetricsDefault];//背景图处理颜色
self.navigationBar.shadowImage = [self createImageWithColor:UIColor.grayColor];//添加阴影,前提是调用了setBackgroundImage:,不然无效

self.navigationBar.tintColor = [UIColor greenColor];//按钮及其文字颜色
    //标题文字字体、大小和颜色
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : kColorWithRGBA(26, 26, 26, 1),
NSFontAttributeName : [UIFont fontWithName:@"PingFangSC-Medium" size:18]}];
//调整标题垂直方向的位置
[self.navigationBar setTitleVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsDefault];
//属性为YES,则UINavigationController其topViewControll.view是包含UINavigationBar和UIStatusBar下面覆盖的那片区域的。但若translucent属性为NO,则除非设置controller的extendedLayoutIncludesOpaqueBars属性为YES,topViewControll.view都是不包含此区域的
 [self.navigationBar setTranslucent:YES];

导航条返回按钮位置的调整->自定义返回按钮(还可以做各种文字图片排版)

UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(0, 0, 25,25);
[leftBtn setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];;
//创建UIBarButtonSystemItemFixedSpace
UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//将宽度设为负值
spaceItem.width = -15;
//将两个BarButtonItem都返回给NavigationItem
self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];

warin:自定义了返回按钮会导致自带的侧换全屏返回失效,解决方案自定义导航栏后侧滑返回功能失效
ios 实现自定义状态栏StatusBar 和 导航栏navigationBar 的状态和颜色
Swift - 动态改变状态栏statusBar文字颜色(preferredStatusBarStyle无效问题

你可能感兴趣的:(导航条、状态栏的设置)