我已經實作了一項功能,當您按下UITabBar圖示并viewController1使用其向上滾動時UIScrollView。它作業得很好,但是如果我向下滾動view并停止某處,然后切換到另一個viewController2,然后回傳viewController1并按tabBar圖示 -viewController1將向上滾動,但Large Title永遠不會顯示,我應該再按一次tabBar圖示來顯示它:
我用于向上滾動 VC1 的代碼:
private var biggestTopSafeAreaInset: CGFloat = 0
override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
self.biggestTopSafeAreaInset = max(view.safeAreaInsets.top, biggestTopSafeAreaInset)
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 0 {
let navigationVC = viewController as? UINavigationController
let firstVC = navigationVC?.viewControllers.first as? CurrencyViewController
guard let scrollView = firstVC?.view.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView else { return }
if traitCollection.verticalSizeClass == .compact {
scrollView.setContentOffset(CGPoint(x: 0, y: -view.safeAreaInsets.top, animated: true)
} else {
scrollView.setContentOffset(CGPoint(x: 0, y: -biggestTopSafeAreaInset, animated: true)
}
}
}
我試圖追蹤生命biggestTopSafeAreaInset的不同階段VC1,但它總是有相同的數字——196.0。但是為什么它直到Large TitleviewControllers 切換后才滾動呢?
uj5u.com熱心網友回復:
嘗試在 viewDidLoad 中添加:
view.addSubview(UIView())
這個單行塊大標題導航欄......我不知道為什么,但這個技巧暫時解決了這個問題......
uj5u.com熱心網友回復:
經過一番研究,我發現什么可以解決我的問題。如果您在稍有延遲的情況下呼叫此方法,tabBarController didSelect則在切換 viewController 后可能會看到大標題。但我仍然無法弄清楚它發生的確切原因......
DispatchQueue.main.asyncAfter(deadline: .now() 0.2) {
navigationVC?.navigationBar.sizeToFit()
}
uj5u.com熱心網友回復:
在您的 tableView 中將 contentInsetAdjustmentBehavior 設定為從不
tableView.contentInsetAdjustmentBehavior = .never
在控制器中再次更新導航欄的ui
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.async { [weak self] in
self?.navigationController?.navigationBar.sizeToFit()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489757.html
