UINaviigationBar的使用

navigationBarHidden和navigationBar.hidden的区别

       navigationBarHidden在官方文档上的解释是A Boolean value that indicates whether the navigation bar is hidden

navigationBar在官方文档上的解释是It is permissible to customize the appearance of the navigation bar using the methods and properties of the UINavigationBar class but you must never change its frame, bounds, or alpha values or modify its view hierarchy directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.

意思就是说它可以通过UINavigationBar这个类的属性来自定义的显示navigation bar,但是你不能改变他的坐标,大小,透明度或者改变它的等级.为了展示或者隐藏navigation bar,你应该通过改变navigationBarHidden属性或者调取setNavigationBarHidden:animated:这个方法.

设置导航栏的背景颜色

在iOS 7中,不再使用tintColor属性来设置导航栏的颜色,而是使用barTintColor属性来修改背景色。我们可以在AppDelegate.m文件中的方法didFinishLaunchingWithOptions:里面添加如下代码来修改颜色:

[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];

默认情况下,导航栏的translucent属性为YES,但是在一般的开发过程中设置为NO

[[UINavigationBar appearance] setTranslucent:NO];

在导航栏中使用背景图片

如果希望在导航栏中使用一个图片当做背景,那么你需要提供一个稍微高一点的图片(这样可以延伸到导航栏背后)。导航栏的高度从44 points(88 pixels)变为了64 points(128 pixels)。我们依然可以使用setBackgroundImage:方法为导航栏设置自定义图片。如下代码所示:


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

定制返回按钮的颜色

要想给返回按钮着色,可以使用tintColor属性

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

对比照


如果想要用自己的图片替换V型,可以设置图片的backIndicatorImage和backIndicatorTransitionMaskImage。如下代码所示:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];

[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_btn.png"]];

修改导航栏标题的字体

跟iOS 6一样,我们可以使用导航栏的titleTextAttributes属性来定制导航栏的文字风格。在text attributes字典中使用如下一些key,可以指定字体、文字颜色、文字阴影色以及文字阴影偏移量:

UITextAttributeFont – 字体key

UITextAttributeTextColor – 文字颜色key

UITextAttributeTextShadowColor – 文字阴影色key

UITextAttributeTextShadowOffset – 文字阴影偏移量key


标题效果图

NSShadow *shadow = [[NSShadow alloc] init];

shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];

shadow.shadowOffset = CGSizeMake(0, 1);

[[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,

shadow, NSShadowAttributeName,

[UIFont fontWithName:@"HelveticaNeue-CondensedBlack"size:21.0], NSFontAttributeName, nil]];

修改电池电量条的风格

第一种方法:在iOS 7中,我们可以在每个view controller中overridingpreferredStatusBarStyle

-(UIStatusBarStyle)preferredStatusBarStyle

{

returnUIStatusBarStyleLightContent;

}

第二种:在UIApplication的statusBarStyle方法来设置状态栏,不过,首先需要停止使用View controller-based status bar appearance。在project target的Info tab中,插入一个新的key,名字为View controller-based status bar appearance,并将其值设置为NO

info

然后就可以使用下面的代码来设置状态栏风格

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

隐藏状态栏

- (BOOL)prefersStatusBarHidden

{

returnYES;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 由于最近两个多月,笔者正和小伙伴们忙于对公司新项目的开发,笔者主要负责项目整体架构的搭建以及功能模块的分工。...
    CoderMikeHe阅读 27,275评论 74 270
  • 本文为大地瓜原创,欢迎知识共享,转载请注明出处。虽然你不注明出处我也没什么精力和你计较。作者微信号:christg...
    大地瓜123阅读 4,059评论 0 0
  • 本文为大地瓜原创,欢迎知识共享,转载请注明出处。虽然你不注明出处我也没什么精力和你计较。作者微信号:christg...
    大地瓜123阅读 4,429评论 1 2
  • 概述 信用卡APP主要满足的是持卡用户还款、分期、抢优惠等需求。 此文分析的是有亲爹背景的信用卡APP,案例产品是...
    阿尔伯特陈阅读 8,454评论 0 13
  • 一直以来心里不安于现状,但又浑浑噩噩,想干什么该干什么都没琢磨清楚,以至于该好好学习的时候没努力,该恋爱的年纪...
    夜页阅读 3,971评论 0 0

友情链接更多精彩内容