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左右就差不多的,颜色修改的会比较多

运行效果如下图所示:

你可能感兴趣的:(iOS导航栏使用总结)