NavigationBar 标题没有出现
NavigationBar title doesn't appear
我以编程方式添加 NavigationBar,后来又添加了标题,但它根本没有出现。这里有什么问题?
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
navigationController?.navigationItem.title = "AAA"
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
view.addSubview(navigationBar)
navigationBar 出现,但标题没有。如何解决?
我也试过这个:
navigationBar.topItem?.title = "BBB"
没有了
在显示的视图控制器中设置了导航栏标题。通常这是在视图控制器上加载视图时完成的:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "AAA"
}
编辑: 意识到 OP 正在将 UINavigationBar
添加到 UIViewController
而不是使用标准 UINavigationController
:
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
let navigationItem = UINavigationItem.init(title: "AAA")
navigationBar.items = [navigationItem]
view.addSubview(navigationBar)
希望这对您有所帮助。
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.whiteColor()
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
这段代码对我有用。
更新:Swift 4/Swift 5
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.white
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
我以编程方式添加 NavigationBar,后来又添加了标题,但它根本没有出现。这里有什么问题?
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
navigationController?.navigationItem.title = "AAA"
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
view.addSubview(navigationBar)
navigationBar 出现,但标题没有。如何解决?
我也试过这个:
navigationBar.topItem?.title = "BBB"
没有了
在显示的视图控制器中设置了导航栏标题。通常这是在视图控制器上加载视图时完成的:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "AAA"
}
编辑: 意识到 OP 正在将 UINavigationBar
添加到 UIViewController
而不是使用标准 UINavigationController
:
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
let navigationItem = UINavigationItem.init(title: "AAA")
navigationBar.items = [navigationItem]
view.addSubview(navigationBar)
希望这对您有所帮助。
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.whiteColor()
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
这段代码对我有用。
更新:Swift 4/Swift 5
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.white
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)