我正在玩桌面控制器和导航栏控制器,而且我在设置导航控制器的titleView属性时遇到了问题。我认为这是相当直接的。
设置titleView的代码
let titleImageView = UIImageView(image: #imageLiteral(resourceName: "Logo 2"))
titleImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 34)
titleImageView.contentMode = .scaleAspectFit
navController.navigationItem.titleView = titleImageView
navController.navigationBar.barTintColor = .white//UIColor.rgb(red: 38, green: 58, blue: 147)
完全控制器
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
let homeController = HomeController()
let navController = UINavigationController(rootViewController: homeController)
navController.tabBarItem.image = #imageLiteral(resourceName: "invoice-filled-50")
navController.tabBarItem.selectedImage = #imageLiteral(resourceName: "invoice-filled-50")
navController.tabBarItem.title = "Home"
let redVC = UIViewController()
redVC.view.backgroundColor = .red
redVC.tabBarItem.image = #imageLiteral(resourceName: "money-box-50")
redVC.tabBarItem.selectedImage = #imageLiteral(resourceName: "money-box-filled-50")
redVC.tabBarItem.title = "Save"
let greenVC = UIViewController()
greenVC.view.backgroundColor = .green
greenVC.tabBarItem.image = #imageLiteral(resourceName: "stocks-50")
greenVC.tabBarItem.selectedImage = #imageLiteral(resourceName: "stocks-filled-50")
greenVC.tabBarItem.title = "Invest"
let orangeVC = UIViewController()
orangeVC.view.backgroundColor = .orange
orangeVC.tabBarItem.image = #imageLiteral(resourceName: "notification-50")
orangeVC.tabBarItem.selectedImage = #imageLiteral(resourceName: "notification-filled-50")
orangeVC.tabBarItem.title = "Alerts"
let titleImageView = UIImageView(image: #imageLiteral(resourceName: "Logo 2"))
titleImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 34)
titleImageView.contentMode = .scaleAspectFit
navController.navigationItem.titleView = titleImageView
navController.navigationBar.barTintColor = .white//UIColor.rgb(red: 38, green: 58, blue: 147)
tabBar.tintColor = UIColor.rgb(red: 38, green: 58, blue: 147)
viewControllers = [navController,redVC, greenVC, orangeVC]
}
}
答案 0 :(得分:0)
您必须指定
navigationBar.topItem?.titleView = titleImageView
不
navController.navigationItem.titleView = titleImageView