iOS设置导航栏和状态栏
文章目錄
- iOS 15 之后導(dǎo)航欄背景色的設(shè)置
- 1、狀態(tài)欄設(shè)置
- 1.1、沒有導(dǎo)航欄
- 1.2、有導(dǎo)航欄
- 2、導(dǎo)航欄背景和字體顏色
- 2.1、十六進(jìn)制顏色轉(zhuǎn)RGB
- 2.2、生成純色圖片
- 3、導(dǎo)航欄的另外一種設(shè)置方式
- 4、導(dǎo)航欄右滑返回失效
- 5、導(dǎo)航欄的一些設(shè)置會影響導(dǎo)航欄是否遮擋view
- 6、總結(jié)導(dǎo)航欄和View布局問題
- 6.1、view在導(dǎo)航欄下開始布局
- 6.2、view從(0,0)開始布局,導(dǎo)航欄遮擋view
- 更新
- 7、關(guān)于 View 從導(dǎo)航欄頂部布局(被導(dǎo)航欄遮擋)和從導(dǎo)航欄底部布局的新的理解。
- 7.1屬性 translucent 的官方介紹
- 8、View布局位置
- 9、ScrollView的布局影響
- 10、translucent=NO 的時(shí)候設(shè)置導(dǎo)航欄背景色透明
iOS 15 之后導(dǎo)航欄背景色的設(shè)置
iOS 13 開始新增了 standardAppearance 和 scrollEdgeAppearance 屬性,不過在iOS 15(xcode13)的時(shí)候才真正需要適配。帶滑動視圖的頁面,當(dāng)滑動到最頂部時(shí)顯示后者的屬性,其他時(shí)候顯示前者的屬性。不帶滑動視圖的頁面只顯示前者的屬性。
if (@available(iOS 15.0, *)) {UINavigationBarAppearance *barAppearance = [[UINavigationBarAppearance alloc] init];barAppearance.backgroundImage = backgroundImage;barAppearance.shadowImage = shadowImage;self.navigationController.navigationBar.standardAppearance = barAppearance;self.navigationController.navigationBar.scrollEdgeAppearance = barAppearance;}1、狀態(tài)欄設(shè)置
typedef NS_ENUM(NSInteger, UIStatusBarStyle) {UIStatusBarStyleDefault = 0, // Automatically chooses light or dark content based on the user interface styleUIStatusBarStyleLightContent API_AVAILABLE(ios(7.0)) = 1, // Light content, for use on dark backgroundsUIStatusBarStyleDarkContent API_AVAILABLE(ios(13.0)) = 3, // Dark content, for use on light backgroundsUIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2, } API_UNAVAILABLE(tvos);UIStatusBarStyleDefault // 默認(rèn)狀態(tài)
UIStatusBarStyleLightContent // 狀態(tài)欄文本和圖標(biāo)為白色
UIStatusBarStyleDarkContent // 狀態(tài)欄文本和圖標(biāo)為黑色
另外兩個(gè)已棄用
1.1、沒有導(dǎo)航欄
在 ViewController 中,使用 -(UIStatusBarStyle)preferredStatusBarStyle 方法設(shè)置
// ViewController - (UIStatusBarStyle)preferredStatusBarStyle {return UIStatusBarStyleLightContent; }1.2、有導(dǎo)航欄
self.navigationController.navigationBar.barStyle = UIBarStyleBlack; typedef NS_ENUM(NSInteger, UIBarStyle) {UIBarStyleDefault = 0,UIBarStyleBlack = 1,UIBarStyleBlackOpaque API_DEPRECATED("Use UIBarStyleBlack instead.", ios(2.0, 13.0)) = 1,UIBarStyleBlackTranslucent API_DEPRECATED("Use UIBarStyleBlack and set the translucent property to YES instead.", ios(2.0, 13.0)) = 2, } API_UNAVAILABLE(tvos);UIBarStyleDefault // 狀態(tài)欄文本和圖標(biāo)為黑色
UIBarStyleBlack // 狀態(tài)欄文本和圖標(biāo)為白色
另外兩個(gè)已棄用
2、導(dǎo)航欄背景和字體顏色
// 狀態(tài)欄深色背景淺色字體(狀態(tài)欄字體白色)[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];// 導(dǎo)航欄背景色// self.navigationController.navigationBar.barTintColor = [UIColor blueColor];// 導(dǎo)航欄背景圖片[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff3d3b)] forBarMetrics:UIBarMetricsDefault];// 導(dǎo)航欄標(biāo)題顏色[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];// 導(dǎo)航欄按鈕顏色[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];2.1、十六進(jìn)制顏色轉(zhuǎn)RGB
//3.獲得RGB顏色#define UIColorFromRGBAlpha(rgbValue, rgbAlpha) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:rgbAlpha]#define UIColorFromRGB(rgbValue) UIColorFromRGBAlpha(rgbValue, 1.0f)2.2、生成純色圖片
+(UIImage *)imageWithColor:(UIColor *)aColor {return [self imageWithColor:aColor withFrame:CGRectMake(0, 0, 1, 1)]; }+(UIImage *)imageWithColor:(UIColor *)aColor withFrame:(CGRect)aFrame {UIGraphicsBeginImageContext(aFrame.size);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [aColor CGColor]);CGContextFillRect(context, aFrame);UIImage *img = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return img; }3、導(dǎo)航欄的另外一種設(shè)置方式
[UINavigationBar appearance] 獲取的是當(dāng)前已經(jīng)展示的導(dǎo)航欄,在調(diào)用系統(tǒng)通訊錄的時(shí)候(CNContactPickerViewController)使用這種方式修改通訊錄導(dǎo)航欄樣式
UINavigationBar *navigationBar = [UINavigationBar appearance];// 狀態(tài)欄深色背景淺色字體(狀態(tài)欄字體白色)[navigationBar setBarStyle:UIBarStyleBlack];// 導(dǎo)航欄背景色[navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff3d3b)] forBarMetrics:UIBarMetricsDefault];// 導(dǎo)航欄標(biāo)題顏色[navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];// 導(dǎo)航欄按鈕顏色[navigationBar setTintColor:[UIColor whiteColor]];4、導(dǎo)航欄右滑返回失效
導(dǎo)航欄設(shè)置了左側(cè)按鈕(self.navigationItem.leftBarButtonItem)右滑返回失效
// 在interface聲明代理 <UIGestureRecognizerDelegate>// 自定義左側(cè)按鈕后右滑返回失效,如下代碼恢復(fù)右滑返回功能self.navigationController.interactivePopGestureRecognizer.delegate = self;5、導(dǎo)航欄的一些設(shè)置會影響導(dǎo)航欄是否遮擋view
//設(shè)置導(dǎo)航欄透明,導(dǎo)航欄透明度默認(rèn)為true,設(shè)置為No,view布局從導(dǎo)航欄底部開始[self.navigationController.navigationBar setTranslucent:true];//把背景設(shè)為空,image設(shè)為nil的話會有一個(gè)半透明黑色圖層,設(shè)為一個(gè)沒有內(nèi)容的圖片,導(dǎo)航欄就是透明的了,view的布局從0,0開始,會被遮擋。[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];image為nil的效果
導(dǎo)航欄遮住View問題
6、總結(jié)導(dǎo)航欄和View布局問題
(這里我對translucent屬性的理解有些問題,文底做說明)
6.1、view在導(dǎo)航欄下開始布局
(1)導(dǎo)航欄不透明,navigationBar.translucent 設(shè)為YES(默認(rèn)為YES),設(shè)置背景圖片(不為nil,且有內(nèi)容);
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];// 這里其實(shí) self.navigationController.navigationBar.translucent = NO;// 文底會做說明(2) 導(dǎo)航欄不透明,navigationBar.translucent 設(shè)為NO,導(dǎo)航欄的背景圖片顏色為黑色;
self.navigationController.navigationBar.translucent = NO;(3)導(dǎo)航欄透明,navigationBar.translucent 設(shè)為YES(默認(rèn)為YES),edgesForExtendedLayout 設(shè)為 UIRectEdgeNone。
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];self.edgesForExtendedLayout = UIRectEdgeNone;6.2、view從(0,0)開始布局,導(dǎo)航欄遮擋view
(1)導(dǎo)航欄不透明,上述情況下設(shè)置extendedLayoutIncludesOpaqueBars屬性為YES
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];self.extendedLayoutIncludesOpaqueBars = YES;(2) 導(dǎo)航欄不透明,navigationBar.translucent 設(shè)為NO,導(dǎo)航欄的背景圖片顏色為黑色;
self.navigationController.navigationBar.translucent = NO;self.extendedLayoutIncludesOpaqueBars = YES;(3)導(dǎo)航欄透明
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];更新
--------------------------------------------------------------------更新于21.03.16--------------------------------------------------------------------
7、關(guān)于 View 從導(dǎo)航欄頂部布局(被導(dǎo)航欄遮擋)和從導(dǎo)航欄底部布局的新的理解。
本來打算修改上面的文章,感覺自己走過的彎路保留一下吧,從這里開始。
7.1屬性 translucent 的官方介紹
self.navigationController.navigationBar.translucent下面是文檔說明
When the navigation bar is translucent, configure the edgesForExtendedLayout and extendedLayoutIncludesOpaqueBars properties of your view controller to display your content underneath the navigation bar. If the navigation bar doesn't have a custom background image, or if any pixel of the background image has an alpha value of less than 1.0, the default value of this property is YES. If the background image is completely opaque, the default value of this property is NO. If you set this property to YES and the custom background image is completely opaque, UIKit applies a system-defined opacity of less than 1.0 to the image. If you set this property to NO and the background image is not opaque, UIKit adds an opaque backdrop.第一段是說導(dǎo)航欄為半透明的(translucent=YES),可以通過edgesForExtendedLayout 和 extendedLayoutIncludesOpaqueBars兩個(gè)屬性來控制view從導(dǎo)航欄的頂部還是底部開始布局。(這個(gè)后面再說,先理解第二段)
第二段是說明導(dǎo)航欄透明度(translucent)和背景圖片(backgroundImage)的關(guān)系。
translucent 默認(rèn)值為 YES不指定translucent的值,背景圖為不透明的,translucent自動為NO; 背景圖為透明的,translucent自動為yes; 背景圖不透明,指定translucent為YES,則背景圖自動變?yōu)榘胪该鞯?#xff1b; 背景圖透明,指定translucent為NO,則背景圖變?yōu)椴煌该鞯?#xff1b;或者說,不指定或者先指定translucent的值,不管是YES還是NO,再設(shè)定背景圖,translucent 的值以圖片的透明度為準(zhǔn);
先指定背景圖,再指定translucent,translucent的值以指定的值為準(zhǔn)。
8、View布局位置
// View 從導(dǎo)航欄頂部開始布局,會被導(dǎo)航欄遮擋; translucent=YES, // View 從導(dǎo)航欄底部開始布局。 translucent=NO ,// View 從導(dǎo)航欄底部開始布局。 translucent=YES 且 self.edgesForExtendedLayout = UIRectEdgeNone; // View 從導(dǎo)航欄頂部開始布局,會被導(dǎo)航欄遮擋; translucent=NO 且 self.extendedLayoutIncludesOpaqueBars = YES;9、ScrollView的布局影響
偷個(gè)懶,看這里吧。
導(dǎo)航欄遮住View問題
沒有做處理的話,無論導(dǎo)航欄透明不透明都不會遮擋。
10、translucent=NO 的時(shí)候設(shè)置導(dǎo)航欄背景色透明
這里記錄一個(gè)設(shè)置導(dǎo)航欄透明的方法,這個(gè)改變的是view的透明度,不影響導(dǎo)航欄背景圖片,所以也不會修改 translucent 的值,如果有滑動控制導(dǎo)航欄透明度的需求可以設(shè)置這里。
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];總結(jié)
以上是生活随笔為你收集整理的iOS设置导航栏和状态栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年电工(初级)考试题及电工(初级
- 下一篇: 堆积柱形图