UINavigationController 内 UIHostingVC 中的 SwiftUI 视图:largeTitle 未更改为内联

问题描述 投票:0回答:1

我有一个 SwiftUI 视图,即

List
。嵌入到
UIHostingController
中,本身嵌入到
FriendsViewController
UINavigationController

部分

滚动时,有2个问题:

  • 标题不会更改为内联
  • 背景不正确,默认的 UIKit 行为是预期的

我基本上尝试了我能做的一切,甚至改变了导航栏的外观,但我始终无法达到默认(且良好)的 UIKit 行为,即:

  • 滚动时标题将切换为内联
  • 导航栏的背景变得充满活力

详情:

  • SwiftUI 视图中没有
    NavigationView
    也没有
    NavigationStack

任何帮助将不胜感激:-)!

代码

override func viewDidLoad() {
    super.viewDidLoad()
    
    let hostingController = UIHostingController(rootView: friendsView)
    self.addChild(hostingController)
    hostingController.didMove(toParent: self)
    self.view.addSubview(hostingController.view)
    
    hostingController.view.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        hostingController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
        hostingController.view.topAnchor.constraint(equalTo: self.view.topAnchor),
        hostingController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        hostingController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
    ])
    
    configureNavigation()
}
private func configureNavigation() {
    // Navigation title
    self.title = "Friends"
    
    // Create UIBarButtonItems
    let symbolConfiguration = UIImage.SymbolConfiguration(paletteColors: [.systemOrange, .systemBlue])
    let symbolImage = UIImage(systemName: "person.badge.plus", withConfiguration: symbolConfiguration)
    let addButton = UIBarButtonItem(image: symbolImage, style: .plain, target: self, action: #selector(addFriend))

    // Set the toolbar items
    navigationItem.rightBarButtonItem = addButton
}
swiftui uinavigationcontroller uihostingcontroller
1个回答
0
投票

所以诀窍是不要将 UIHostingController 作为另一个 UIViewController 的子级嵌入。它会阻止滚动和标题一起正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.