UINavigation修改
来源:互联网 发布:容祖儿 借过知乎 编辑:程序博客网 时间:2024/05/24 02:58
一、无导航条的情况:
系统默认状态栏的字体颜色为黑色,即UIStatusBarStyle=UIStatusBarStyleDefault,同时背景颜色和self.view.backgroundColor颜色一致,如下图所示:
假如我想让状态栏颜色设置成红色,字体仍为黑色,可以在需要显示的那一页进行如下设置:(最好写在viewWillAppear里面)
//设置状态栏颜色- (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; }}- (void)viewDidLoad { [super viewDidLoad]; [self setStatusBarBackgroundColor:[UIColor redColor]]; self.view.backgroundColor = [UIColor yellowColor];}
效果如下:
假如此时我想让状态栏文字颜色变成白色,可以这样操作:
在上面代码的基础上再添加下面一段代码:
- (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}
效果如下:
问题来了,当你在这一页点击按钮进入下一页后,状态栏背景颜色不变,还是红色,而字体颜色却变成黑色了,比较闹心!可以通过下面的方法随心所欲的在任意一页修改状态栏的字体颜色(字体颜色只有白色和黑色)和背景颜色(直接复制到项目中即可
//设置字体颜色- (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;//白色}//设置状态栏颜色- (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; }}//!!!重点在viewWillAppear方法里调用下面两个方法-(void)viewWillAppear:(BOOL)animated{ [self preferredStatusBarStyle]; [self setStatusBarBackgroundColor:[UIColor redColor]];}- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; }
下面的效果是第一页要红底白字,第二页要绿底黑字,返回后也是正常显示
二、有导航条的情况
当我在上面的基础上添加了导航条后,会发现字体颜色由之前的白色变成黑色了,背景颜色倒没有发生变化
不用担心,可以通过下面的方法完美解决:
//设置状态栏颜色- (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; }}-(void)viewWillAppear:(BOOL)animated{ [self setStatusBarBackgroundColor:[UIColor redColor]]; [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;}--->!!!同时别忘了在info plist里面将View controller-based status bar appearance设置成NO,(默认是YES)
现在基本的设备都适配ios7以上设备,默认的状态栏字体颜色是黑色[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleDefault;现在基本的设备都适配ios7以上设备,默认的状态栏字体颜色是黑色[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
Demo下载地址:https://github.com/zhuchenglong/StatusBarDemo
以下是我在实际项目中使用的:
//设置状态栏颜色- (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; NSLog(@"statusBar.backgroundColor--->%@",statusBar.backgroundColor); if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; }}- (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;//白色}- (void)viewDidLoad { [super viewDidLoad]; //Y起点在导航条下面 self.edgesForExtendedLayout = UIRectEdgeNone; //设置navigationItem返回的文字 UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil]; self.navigationItem.backBarButtonItem = item;}-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //设置导航条透明度 self.navigationController.navigationBar.translucent = NO;//不透明 [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1]; //图标颜色为黑色 [self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; //导航栏背景颜色 [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]]; //导航条下面的黑线 self.navigationController.navigationBar.clipsToBounds = NO; //刷新状态栏背景颜色 // [self setNeedsStatusBarAppearanceUpdate]; //设置状态栏颜色 [self setStatusBarBackgroundColor:[UIColor blackColor]];}//一定要在viewWillDisappear里面写,如果写在viewDidDisappear里面会出问题!!!!- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //为了不影响其他页面在viewDidDisappear做以下设置 self.navigationController.navigationBar.translucent = YES;//透明 [self setStatusBarBackgroundColor:[UIColor clearColor]];}
效果:
三、UI元素都往上漂移了44pt
iOS 7以后,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。
解决方案一: edgesForExtendedLayout
默认值不是UIRectEdgeAll
吗,修改为 UIRectEdgeNone
就OK了
注意修改这个属性是要用UIViewController来修改,UINavigationController
,UITabBarController
修改无效。
官方解释: This property is applied only to view controllers that are embedded in a container such as UINavigationController. The window’s root view controller does not react to this property. The default value of this property is UIRectEdgeAll.
解决方案二:
造成偏移的原因是你的 navigationBar.translucent
默认值为 YES ,此时有半透明效果。如果这个效果是非必须的 改为NO,也可以解决,两者相互独立,互不冲突。
# UIViewController.h@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll# UINavigationBar.h@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
- UINavigation修改
- UINavigation返回按钮修改标题
- UINavigation
- iOS 全局修改UINavigation 后退按钮
- iOS 全局修改UINavigation 后退按钮
- iOS 全局修改UINavigation 后退按钮
- 如果UINavigation的背景色被修改,返回也要修改的话可以这样做
- UINavigation Controller
- UINavigation Controller
- UITabBar&UINavigation
- iOS UINavigation
- UINavigation详解
- UINavigation相关
- IOS导航栏UINavigation
- UINavigation自定义返回按钮
- UINavigation Controler 切换效果
- ios UINavigation 用法示例
- UINavigation category 定制
- javascript中的json对象应用
- C++笔记 --入门
- linux查看服务器设备信息命令dmidecode
- 批处理 打开指定的文件夹
- iOS
- UINavigation修改
- birthday
- 先验概率、似然函数、后验概率、贝叶斯公式
- linux wc命令学习
- 链表的有序集合
- 最短路练习5/poj/1860 /Currency Exchange 有正环
- oracle数据库表空间.DBF文件被手动删除(直接找),然后就登录不了数据库的解决办法
- Linux入门(5)——Ubuntu16.04安装网易云音乐
- rdis 缓存