我的應用程式有一個帶有 5 個選項卡的主 UITabBarController(每個選項卡都是一個 UINavigationController)。選項卡包含各種 UI 元素,例如 UITableViews(帶有和不帶有自定義單元格)、UISegmentedControls 等。當螢屏上顯示視圖/選項卡并且我旋轉設備時,布局會按預期進行調整,并且一切正常。但是,現在我執行以下操作:
- 轉到任何選項卡,例如選項卡 A
- 導航到另一個選項卡,比如說選項卡 B
- 仍在選項卡 B 上時,旋轉設備,例如從縱向到橫向
- 在仍處于新方向時回傳選項卡 A
選項卡 A 的視圖布局現在搞砸了。例如,tableview 頂部有很大的邊距,或者 UISegmentedControl 根本不可見。對于 UITableView,布局最終會在我滾動一點時更新。對于其他視圖,獲得正確布局的唯一可能性是在視圖可見時再次旋轉設備。
有些視圖是通程序式創建的,有些是在故事板中創建的。因此,選擇哪種方法似乎并不重要,兩者都存在問題。可以在
選擇選項卡2:

旋轉裝置:

選擇 Tab1:

如我們所見,標簽上方有一個“間隙”。
viewWillAppear()現在,編輯TabOneNavigationController:
override func viewWillAppear(_ animated: Bool) {
// add this line to fix the layout problem
super.viewWillAppear(animated)
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = .blue
navigationBar.standardAppearance = navBarAppearance
navigationBar.scrollEdgeAppearance = navBarAppearance
}
}
再次運行它,當我們回到 Tab1 時:

附帶說明:您可以將navBarAppearance代碼放入viewDidLoad()其中,這樣它就只運行一次,而不是每次切換選項卡時運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496756.html
標籤:IOS 迅速 自动布局 uitabbar控制器 方向
