ios - 导航栏在更改为内联之前作为大标题短暂闪烁
问题描述
我在我的 SwiftUI 应用程序中使用了 UITabController,它运行良好,除了一个问题。
当我有一个使用大标题的 NavigationView 时,它会在我滚动时变为内联,正如预期的那样。到目前为止,一切都很好。
当我切换标签,然后再次切换回来时,它应该记住滚动位置,因此是内联的。相反,它在移动到内联之前非常短暂地闪烁为一个大标题。
这似乎只发生在设备上。
有人可以帮助我解决问题吗?
struct ContentView: View {
var body: some View {
CustomTabView([
Tab(view: FirstView(),
barItem: UITabBarItem(title: "First", image: UIImage(systemName: "1.circle"), selectedImage: nil)),
Tab(view: SecondView(),
barItem: UITabBarItem(title: "Second", image: UIImage(systemName: "2.circle"), selectedImage: nil))
])
}
}
struct FirstView: View {
var body: some View {
NavigationView {
List {
ForEach( 0...40, id: \.self ) {
Text("\($0)")
.padding()
}
}
.navigationTitle("First View")
}
}
}
struct SecondView: View {
var body: some View {
NavigationView {
List {
ForEach( 41...80, id: \.self ) {
Text("\($0)")
.padding()
}
}
.navigationTitle("Second View")
}
}
}
struct TabBarController: UIViewControllerRepresentable {
var controllers: [UIViewController]
func makeUIViewController(context: Context) -> UITabBarController {
let tabBarController = UITabBarController()
tabBarController.viewControllers = controllers
tabBarController.delegate = context.coordinator
return tabBarController
}
func updateUIViewController(_ uiViewController: UITabBarController, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let fromView = tabBarController.selectedViewController?.view, let toView = viewController.view else {
return false
}
if fromView != toView {
fromView.superview!.addSubview(toView)
}
return true
}
}
}
struct CustomTabView: View {
var viewControllers: [UIHostingController<AnyView>]
init(_ tabs: [Tab]) {
self.viewControllers = tabs.map {
let host = UIHostingController(rootView: $0.view)
host.tabBarItem = $0.barItem
return host
}
}
var body: some View {
TabBarController(controllers: viewControllers)
.edgesIgnoringSafeArea(.all)
}
}
struct Tab {
var view: AnyView
var barItem: UITabBarItem
init<V: View>(view: V, barItem: UITabBarItem) {
self.view = AnyView(view)
self.barItem = barItem
}
}
解决方案
我找到了解决方法/修复。
我需要使用自定义 UIHostingController,找到导航控制器,然后在 viewWillAppear 函数中添加一行:
public class MyHostingController: UIHostingController<AnyView> {
override open func viewWillAppear(_ animated: Bool) {
if let navigationController = navigationController() {
navigationController.view.setNeedsUpdateConstraints()
}
super.viewWillAppear(animated)
}
}
private extension UIViewController {
func navigationController() -> UINavigationController? {
var controller: UINavigationController?
if let navigationController = self as? UINavigationController {
return navigationController
}
children.forEach {
if let navigationController = $0 as? UINavigationController {
controller = navigationController
} else {
controller = $0.navigationController()
}
}
return controller
}
}
然后显然用 MyHostingController 替换了我原始代码中 UIHostingController 的两种用法。
最重要的一行是:
navigationController.view.setNeedsUpdateConstraints()
希望这可以帮助其他发现类似问题的人。
推荐阅读
- python - 如何设置特定的 x 轴标签以在熊猫箱线图上显示非字母顺序?
- reactjs - 空间与网格:何时使用?
- c# - ListCollections 与 autoizedcollection
- r - 如何将大文件读入R环境
- python - 在 Window 上下载 Tensorflow 时出现环境错误
- python - 在 OpenCV DNN 模块中缓存加载的模型
- maven - Maven 模块项目在 Maven 中复制模块 JAR(使用 IntelliJ)
- scala - Spark Scala - rdd distinct nullpointerexception
- asp.net-mvc - 引导模式(弹出窗口)在 ASP.NET MVC 中不起作用
- r - 如何找到时间序列中缺失的时间序列或不规则性?