navigationbar的一些事
1.iOS7之后 要想改变navigationbar的颜色 可以这样子改
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#3A3A3A" alpha:1.0f];
默认带有一定透明效果,可以使用以下方法去除系统效果
[self.navigationController.navigationBar setTranslucent:NO];
设置self.automaticallyAdjustsScrollViewInsets = NO,我的controller继承于UITableViewController,然后tableview下面被遮住了20像素,解决方案
self.edgesForExtendedLayout = UIRectEdgeNone;就可以正常self.automaticallyAdjustsScrollViewInsets
导航视图内Push进来的以“TableView”(没有ScrollView截图,就将就一下)为主View的视图,本来我们的cell是放在(0,0)的位置上的,但是考虑到导航栏、状态栏会挡住后面的主视图,而自动把我们的内容(cell、滚动视图里的元素)向下偏移离Top64px(下方位置如果是tarbar向上偏移离Buttom49px、toolbar是44),也就是当我们把navigationBar给隐藏掉时,滚动视图会给我们的内容预留部分的空白Top(所有内容向下偏移20px,因为状态栏的存在)。出来的效果可以脑补一下。
那么,当我们不想自动为我们下移可以设置:
复制代码
|
这样我们的内容就不会自动偏移了,例如上面的cell就是从(0,0)的位置开始。
很多人在Nib或者Storyboard中调整视图时,因为IB中有NavigationBar的存在,误导许多人将加入scrollviewB的高度设置为根视图viewA的高度,并且加入的控件、子视图、cell等等都是从viewB的(0,64)
这样在有navigation下的公洞视图在添加按钮等控件的时候就不会偏移64了!!
2.改变UINavigationBar导航条标题颜色和字体
iOS 5 以后 UINavigationController 可以 改变UINavigationBar导航条标题颜色和字体
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0 green:0.7 blue:0.8 alpha:1], NSForegroundColorAttributeName,[UIColor colorWithRed:0 green:0.7 blue:0.8 alpha:1],NSShadowAttributeName,[NSValue valueWithUIOffset:UIOffsetMake(0, 0)], NSShadowAttributeName,[UIFont fontWithName:@"Arial-Bold" size:0.0], NSFontAttributeName,nil]];
其中 NSForegroundColorAttributeName和NSFontAttributeName 属性是文字颜色和字体
3.改变状态栏的颜色
公司项目需要将状态栏的文字颜色设置为白色,以下方法即可
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
改变后需要及时刷新的调用
[viewController setNeedsStatusBarAppearanceUpdate];
如果没有效果,需要在plist文件里设置
View controller-based status bar appearance = NO
info.plist中 View controller-based status bar appearance这个属性 View controller-based status bar appearance =NO 这个设置为:View Controller 不对status Bar 显示进行操作
4.在某些页面控制navigationbar不显示
在项目中有时候会遇到在有些页面想隐藏navgationbar的情况,只要像这样写就可以了
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
5.图片选择器调用时候有关导航条的问题
有时候调用系统相册的时候,如果我们整体导航条背景颜色是通过一张图片进行加载的话 那么相册调用出来后,导航条的颜色也是会加载这张图片的,但是如果整条导航填颜色是通过颜色色值来加载的话,这时候系统相册调用出来的颜色就是系统默认的,导致白色导航条,白色文字就会显示不出来,从而影响用户体验;还有一种情况是,调用系统相册之后,导航条上的文字,和取消按钮都是显示的英文,跟我们整体风格格格不入,因此我们需要重写相册导航条上的内容、图片和文字以及文字颜色.
(1)如果整体导航条是按照色值来的,修改方法
// 实现navigationController的代理
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.navigationController.navigationBar.barTintColor = MainAppColor;
[viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica Neue" size:19.0f], NSFontAttributeName,nil]];
viewController.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
(2)如果显示出来的是英文 我们想改成中文的
在调用系统相册、相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文,在info.plist设置解决问题 info.plist里面添加Localized resources can be mixed YES 表示是否允许应用程序获取框架库内语言。