我正在尝试将ImageView添加到NavigationBar中。我在SceneDelegate中做了所有的设置来设置一个rootview控制器,它看起来很有效,但是当我试图添加标题或图像时,它并没有显示出来。
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
}
}
}发布于 2022-04-19 06:04:23
不要将UITabBarController嵌入到UINavigationController中。而是将每个UIViewController嵌入到UINavigationController中,然后再将它们添加到UITabBarController中。下面是一个例子。
修改scene(_:willConnectTo:options)方法如下所示。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = MainTabViewController()
self.window = window
window.makeKeyAndVisible()
}这是MainTabBarController类。
class MainTabViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let mainVC = UINavigationController(rootViewController: ViewController())
mainVC.tabBarItem = UITabBarItem(title: "Tab 1", image: UIImage(systemName: "circle"), selectedImage: UIImage(systemName: "circle.fill"))
let secondVC = UINavigationController(rootViewController: SecondViewController())
secondVC.tabBarItem = UITabBarItem(title: "Tab 2", image: UIImage(systemName: "square"), selectedImage: UIImage(systemName: "square.fill"))
viewControllers = [mainVC, secondVC]
}
}现在修改titleView of navigationItem in UIViewController。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
setupNavController()
}
func configureUI() {
view.backgroundColor = .white
let imageView = UIImageView(image: UIImage(systemName: "sun.max.circle.fill"))
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
func setupNavController() {
if #available(iOS 15.0, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
}
}
}https://stackoverflow.com/questions/71919784
复制相似问题