iOS導(dǎo)航欄對控制器view的影響詳解
前言
當(dāng)我們設(shè)置導(dǎo)航欄的某些屬性的時候會導(dǎo)致控制器View的布局不是從window的 (0,0)點開始布局,會從導(dǎo)航欄底部開始布局,而此時在 viewDidLoad 中 獲取到View的frame 確實從(0,0)開始的,只有在 viewDidAppear中才能獲取到 view 最終的實際 frame
一些屬性
在了解 UINavigationBar之前,有必要了解 UINavigationBar 的一些屬性
///默認 default 半透明 black 黑色 open var barStyle: UIBarStyle // 底部陰影橫線,默認nil // 官方解釋還涉及到了一個設(shè)置背景圖片的方法 -setBackgroundImage:forBarMetrics: open var shadowImage: UIImage? // 7.0 以后已經(jīng)改變,修改bar 背景顏色 請使用 -barTintColor open var tintColor: UIColor! // default is nil bar 的背景顏色 open var barTintColor: UIColor? /// 影響比較大的屬性見下文,是否是半透明的 open var isTranslucent: Bool // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
一些條件
///當(dāng)前 控制器并不是 tableviewcontroller self.view.backgroundColor = .cyan self.tableView.backgroundColor = .red self.navigationItem.title = "rootVC 標(biāo)題" tableView.frame = view.bounds
1.1 默認導(dǎo)航欄 帶有半透明效果
此時view 和 tableview 和 導(dǎo)航欄布局
1 view全屏布局
2 tableview默認從導(dǎo)航欄下部開始布局
3 導(dǎo)航欄半透明
細節(jié) : 此時導(dǎo)航欄中的 _UIVisualEffectBackdropView 屬性變成紅色即 tableview的背景色
1.2 此時如果想讓tableview 從頂部開始布局可添加代碼
if #available(iOS 11.0,*) { self.tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never; } else { self.automaticallyAdjustsScrollViewInsets = false; }
神奇的是 如果 tableview從頂部布局 此時導(dǎo)航欄中的 _UIVisualEffectBackdropView 屬性又會變成默認白色
2 設(shè)置導(dǎo)航欄 isTranslucent屬性
isTranslucent 在6.0以后默認是 true
如果設(shè)置為false
self.navigationController?.navigationBar.isTranslucent = false
此時布局
1 view 從導(dǎo)航欄底部布局
2 tableview 從view (0,0) 布局
3 導(dǎo)航欄不透明 _UIBarBackground 默認為白色
3.1設(shè)置barTintColor
self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.navigationBar.barTintColor = UIColor.purple
此時布局和默認一樣
1 view從 (0,0)布局
2 tableview從導(dǎo)航欄底部布局
3 導(dǎo)航欄半透明
不同的是 UIVisualEffectView多加了一個 _UIVisualEffectSubview 用來顯示我們自定義的背景色
其他兩個 _UIVisualEffectSubview 和 _UIVisualEffectBackdropView view 用來實現(xiàn)半透明效果
3.2在 barTintColor基礎(chǔ)上設(shè)置 isTranslucent = false 屬性
結(jié)果 和 2 中的效果一樣。不同的是
_UIBarBackground 變成了我們自定義的顏色
4.1 設(shè)置 setBackgroundImage
設(shè)置一張純色圖片
self.navigationBar.setBackgroundImage(UIColor.mm_colorImgHex(color_vaule: hex,alpha: 1), for: UIBarPosition.any, barMetrics: .default)
此時 布局
1 view 從導(dǎo)航欄底部布局 view---(0.0, 88.0, 414.0, 808.0)
2 tableview 從(0,0) 布局
3 導(dǎo)航欄不透明
此時打印導(dǎo)航欄 isTranslucent屬性 為false也就是說如果調(diào)用了setBackgroundImage會默認 將 isTranslucent 置位 false
translate-----Optional(false)
4.2 我們在4.1的情況下 修改 isTranslucent
在 viewWillAppear 中修改 isTranslucent 為 true
此時布局
1 view 全屏布局
2 tableview從導(dǎo)航欄底部頂部開始布局
3 導(dǎo)航欄透明
此時打印我們的 _UIBarBackground 中的 BackgroundImage 透明度已被修改
<UIImageView: 0x7fbef1f0ce10; frame = (0 0; 414 88); alpha = 0.909804; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x600000cabd00>>
總結(jié)
由此所有情況都已測試完畢
1 view 的大小總是被導(dǎo)航欄的 isTranslucent屬性影響
2 修改 setBackgroundImage 會影響到 isTranslucent屬性。
3 修改barTintColor 屬性 NavigationBar 會為我們在 _UIVisualEffectView 中添加一個 我們自定義顏色的 _UIVisualEffectSubView
DEMO在這
歡迎指點Demo
題外話
通過查資料和 測試
關(guān)于 setBackgroundImage中的 UIBarMetrics參數(shù)
1 default // 橫屏豎屏都顯示
2 compact //表示在只橫屏下才顯示,和UIBarMetricsLandscapePhone功效一樣,不過iOS8已經(jīng)棄用了
3 defaultPrompt & compactPrompt 均無效果 不知道如何起作用
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS App設(shè)計模式開發(fā)中對建造者模式的運用實例
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對建造者模式的運用實例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié)
Xcode環(huán)境中包含CoreMotion框架,能夠幫助我們調(diào)用硬件設(shè)備的加速度傳感器和陀螺儀等感應(yīng)器,下面比較詳細地整理了iOS App中調(diào)用iPhone各種感應(yīng)器的方法總結(jié),需要的朋友可以參考下:2016-07-07