iOS设置导航栏和状态栏
文章目錄
- iOS 15 之后導航欄背景色的設置
- 1、狀態欄設置
- 1.1、沒有導航欄
- 1.2、有導航欄
- 2、導航欄背景和字體顏色
- 2.1、十六進制顏色轉RGB
- 2.2、生成純色圖片
- 3、導航欄的另外一種設置方式
- 4、導航欄右滑返回失效
- 5、導航欄的一些設置會影響導航欄是否遮擋view
- 6、總結導航欄和View布局問題
- 6.1、view在導航欄下開始布局
- 6.2、view從(0,0)開始布局,導航欄遮擋view
- 更新
- 7、關于 View 從導航欄頂部布局(被導航欄遮擋)和從導航欄底部布局的新的理解。
- 7.1屬性 translucent 的官方介紹
- 8、View布局位置
- 9、ScrollView的布局影響
- 10、translucent=NO 的時候設置導航欄背景色透明
iOS 15 之后導航欄背景色的設置
iOS 13 開始新增了 standardAppearance 和 scrollEdgeAppearance 屬性,不過在iOS 15(xcode13)的時候才真正需要適配。帶滑動視圖的頁面,當滑動到最頂部時顯示后者的屬性,其他時候顯示前者的屬性。不帶滑動視圖的頁面只顯示前者的屬性。
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、狀態欄設置
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 // 默認狀態
UIStatusBarStyleLightContent // 狀態欄文本和圖標為白色
UIStatusBarStyleDarkContent // 狀態欄文本和圖標為黑色
另外兩個已棄用
1.1、沒有導航欄
在 ViewController 中,使用 -(UIStatusBarStyle)preferredStatusBarStyle 方法設置
// ViewController - (UIStatusBarStyle)preferredStatusBarStyle {return UIStatusBarStyleLightContent; }1.2、有導航欄
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 // 狀態欄文本和圖標為黑色
UIBarStyleBlack // 狀態欄文本和圖標為白色
另外兩個已棄用
2、導航欄背景和字體顏色
// 狀態欄深色背景淺色字體(狀態欄字體白色)[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];// 導航欄背景色// self.navigationController.navigationBar.barTintColor = [UIColor blueColor];// 導航欄背景圖片[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff3d3b)] forBarMetrics:UIBarMetricsDefault];// 導航欄標題顏色[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];// 導航欄按鈕顏色[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];2.1、十六進制顏色轉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、導航欄的另外一種設置方式
[UINavigationBar appearance] 獲取的是當前已經展示的導航欄,在調用系統通訊錄的時候(CNContactPickerViewController)使用這種方式修改通訊錄導航欄樣式
UINavigationBar *navigationBar = [UINavigationBar appearance];// 狀態欄深色背景淺色字體(狀態欄字體白色)[navigationBar setBarStyle:UIBarStyleBlack];// 導航欄背景色[navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0xff3d3b)] forBarMetrics:UIBarMetricsDefault];// 導航欄標題顏色[navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];// 導航欄按鈕顏色[navigationBar setTintColor:[UIColor whiteColor]];4、導航欄右滑返回失效
導航欄設置了左側按鈕(self.navigationItem.leftBarButtonItem)右滑返回失效
// 在interface聲明代理 <UIGestureRecognizerDelegate>// 自定義左側按鈕后右滑返回失效,如下代碼恢復右滑返回功能self.navigationController.interactivePopGestureRecognizer.delegate = self;5、導航欄的一些設置會影響導航欄是否遮擋view
//設置導航欄透明,導航欄透明度默認為true,設置為No,view布局從導航欄底部開始[self.navigationController.navigationBar setTranslucent:true];//把背景設為空,image設為nil的話會有一個半透明黑色圖層,設為一個沒有內容的圖片,導航欄就是透明的了,view的布局從0,0開始,會被遮擋。[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];image為nil的效果
導航欄遮住View問題
6、總結導航欄和View布局問題
(這里我對translucent屬性的理解有些問題,文底做說明)
6.1、view在導航欄下開始布局
(1)導航欄不透明,navigationBar.translucent 設為YES(默認為YES),設置背景圖片(不為nil,且有內容);
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];// 這里其實 self.navigationController.navigationBar.translucent = NO;// 文底會做說明(2) 導航欄不透明,navigationBar.translucent 設為NO,導航欄的背景圖片顏色為黑色;
self.navigationController.navigationBar.translucent = NO;(3)導航欄透明,navigationBar.translucent 設為YES(默認為YES),edgesForExtendedLayout 設為 UIRectEdgeNone。
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];self.edgesForExtendedLayout = UIRectEdgeNone;6.2、view從(0,0)開始布局,導航欄遮擋view
(1)導航欄不透明,上述情況下設置extendedLayoutIncludesOpaqueBars屬性為YES
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];self.extendedLayoutIncludesOpaqueBars = YES;(2) 導航欄不透明,navigationBar.translucent 設為NO,導航欄的背景圖片顏色為黑色;
self.navigationController.navigationBar.translucent = NO;self.extendedLayoutIncludesOpaqueBars = YES;(3)導航欄透明
// self.navigationController.navigationBar.translucent = YES;[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];更新
--------------------------------------------------------------------更新于21.03.16--------------------------------------------------------------------
7、關于 View 從導航欄頂部布局(被導航欄遮擋)和從導航欄底部布局的新的理解。
本來打算修改上面的文章,感覺自己走過的彎路保留一下吧,從這里開始。
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.第一段是說導航欄為半透明的(translucent=YES),可以通過edgesForExtendedLayout 和 extendedLayoutIncludesOpaqueBars兩個屬性來控制view從導航欄的頂部還是底部開始布局。(這個后面再說,先理解第二段)
第二段是說明導航欄透明度(translucent)和背景圖片(backgroundImage)的關系。
translucent 默認值為 YES不指定translucent的值,背景圖為不透明的,translucent自動為NO; 背景圖為透明的,translucent自動為yes; 背景圖不透明,指定translucent為YES,則背景圖自動變為半透明的; 背景圖透明,指定translucent為NO,則背景圖變為不透明的;或者說,不指定或者先指定translucent的值,不管是YES還是NO,再設定背景圖,translucent 的值以圖片的透明度為準;
先指定背景圖,再指定translucent,translucent的值以指定的值為準。
8、View布局位置
// View 從導航欄頂部開始布局,會被導航欄遮擋; translucent=YES, // View 從導航欄底部開始布局。 translucent=NO ,// View 從導航欄底部開始布局。 translucent=YES 且 self.edgesForExtendedLayout = UIRectEdgeNone; // View 從導航欄頂部開始布局,會被導航欄遮擋; translucent=NO 且 self.extendedLayoutIncludesOpaqueBars = YES;9、ScrollView的布局影響
偷個懶,看這里吧。
導航欄遮住View問題
沒有做處理的話,無論導航欄透明不透明都不會遮擋。
10、translucent=NO 的時候設置導航欄背景色透明
這里記錄一個設置導航欄透明的方法,這個改變的是view的透明度,不影響導航欄背景圖片,所以也不會修改 translucent 的值,如果有滑動控制導航欄透明度的需求可以設置這里。
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];總結
以上是生活随笔為你收集整理的iOS设置导航栏和状态栏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年电工(初级)考试题及电工(初级
- 下一篇: 堆积柱形图