Swift 5.6 NavigationItem不显示
我正在尝试将ImageView添加到NavigationBar。我在SceneDelegate中进行了所有设置,以设置一个rootviewController,并且似乎有效,但是当我尝试添加标题或图像时,它确实没有显示。
SceneDelegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
window?.rootViewController = UINavigationController(rootViewController: MainTabController())
window?.makeKeyAndVisible()
}
navigationbar NavigationController:
class FeedController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
setupNavController()
}
func configureUI() {
view.backgroundColor = .white
let imageView = UIImageView(image: UIImage(named: "twitter_logo_blue"))
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
@objc func addTapped() {
//
}
func setupNavController() {
if #available(iOS 15.0, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
}
}
}
I am trying to add ImageView to NavigationBar. I did all setups in SceneDelegate to setup a rootviewcontroller and seems like it works, but when I try to add Title or Image it does no show it.
SceneDelegate :
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
window?.rootViewController = UINavigationController(rootViewController: MainTabController())
window?.makeKeyAndVisible()
}
NavigationBar
NavigationController:
class FeedController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
setupNavController()
}
func configureUI() {
view.backgroundColor = .white
let imageView = UIImageView(image: UIImage(named: "twitter_logo_blue"))
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
@objc func addTapped() {
//
}
func setupNavController() {
if #available(iOS 15.0, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请勿在
uinavigationController
中嵌入uitabbarController
。宁愿将每个uiviewController
嵌入uinavigationController
之前,然后将它们添加到uitabbarcontroller
。这是一个例子。修改
场景(_:willconnectto:options)
方法如下。这是
MainterabbarController
类。现在,
navigationItem
在uiviewController
中修改titleView
navigationItem 。Do not embed
UITabBarController
inUINavigationController
. Rather embed eachUIViewController
intoUINavigationController
before adding them toUITabBarController
. Here is an example.Modify the
scene(_:willConnectTo:options)
method like below.Here is the
MainTabBarController
class.Now modify
titleView
ofnavigationItem
inUIViewController
.