我有一个 SwiftUI 视图,即
List
。嵌入到 UIHostingController
中,本身嵌入到 FriendsViewController
的
UINavigationController
部分
滚动时,有2个问题:
我基本上尝试了我能做的一切,甚至改变了导航栏的外观,但我始终无法达到默认(且良好)的 UIKit 行为,即:
详情:
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
}
所以诀窍是不要将 UIHostingController 作为另一个 UIViewController 的子级嵌入。它会阻止滚动和标题一起正常工作。