iOS导航栏使用总结

iOS 设置导航栏透明,修改title字体颜色,字体大小


为了对比相较明显,给一个背景颜色,贴出关键代码

self.view.backgroundColor = [UIColoryellowColor];self.navigationItem.title =@"这是一个Title";

显示如图所示:(这是系统默认的,不作任何修改)

FC1A9D02-3E25-4E5B-AEE9-EEAA883629BE.png

设置导航栏透明,贴出关键代码

self.view.backgroundColor = [UIColoryellowColor];self.navigationItem.title =@"这是一个Title";//设置导航栏背景图片为一个空的image,这样就透明了[self.navigationController.navigationBar setBackgroundImage:[[UIImagealloc] init] forBarMetrics:UIBarMetricsDefault];//去掉透明后导航栏下边的黑边[self.navigationController.navigationBar setShadowImage:[[UIImagealloc] init]];self.navigationController.navigationBar.translucent =YES;

显示如下所示:

8091D90C-A9A7-46D1-A876-1158455B287E.png

这是说下这个translucent属性,翻看API如下所示:

/*

New behavior on iOS 7.

Default is YES.

You may force an opaque background by setting the property to NO.

If the navigation bar has a custom background image, the default is inferred

from the alpha values of the image—YES if it has any pixel with alpha < 1.0

If you send setTranslucent:YES to a bar with an opaque custom background image

it will apply a system opacity less than 1.0 to the image.

If you send setTranslucent:NO to a bar with a translucent custom background image

it will provide an opaque background for the image using the bar's barTintColor if defined, or black

for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.

*/@property(nonatomic,assign,getter=isTranslucent)BOOLtranslucentNS_AVAILABLE_IOS(3_0)UI_APPEARANCE_SELECTOR;// Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

iOS 6 以及 6以前这个属性默认是NO,iOS 7 默认是YES,如果接触的是iOS 7以后的可以不用写的,这是说一个小插曲,前面我接手的项目,有一个controller需要设置导航栏透明,没写最后一句,发现有问题,最后检查是由于父类的controller里面设置了该属性为NO,所以建议最后一句代码还是写上更为保险点,这也算是采坑之后的经验吧

设置title的字体以及颜色,关键代码如下所示:

self.view.backgroundColor = [UIColoryellowColor];self.navigationItem.title =@"这是一个Title";//设置导航栏背景图片为一个空的image,这样就透明了[self.navigationController.navigationBar setBackgroundImage:[[UIImagealloc] init] forBarMetrics:UIBarMetricsDefault];//去掉透明后导航栏下边的黑边[self.navigationController.navigationBar setShadowImage:[[UIImagealloc] init]];self.navigationController.navigationBar.translucent =YES;    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColorredColor],NSFontAttributeName: [UIFontfontWithName:@"Helvetica-Bold"size:17]}];

可以根据求修改字体大小,一般17左右就差不多的,颜色修改的会比较多

运行效果如下图所示:

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自:swift3访问控制 从低到高的顺序为 private 仅在当前类访问 fileprivate 仅在当前sw...
    whlpkk阅读 1,846评论 0 1
  • 前四章标题 第一章讲的是-A的价值。 什么是-A?即与你想推荐或推销的物品相似但稍差一些的参照。它的价值则是在有三...
    Oo呢喃oO阅读 3,465评论 0 1
  • 备注:原文写于2017年,本文在原文的基础上进一步加工。时至今日,里面的很多技能已经很熟练了,贴在这里,提醒自己,...
    青风如梦阅读 1,534评论 0 0