在 Swift 中更改导航栏

Changing the Navigation Bar in Swift

我只是想知道如何使我的导航栏看起来像下图中左侧的导航栏:

我的应用程序使用导航控制器,默认情况下导航栏看起来像图像右侧的屏幕。我不喜欢它这么小,希望它更高,并且能够更改用于 header 的文本的颜色和字体。

对于如何执行此操作以及在何处放置代码,我将不胜感激。

谢谢

有很多方法可以自定义您的导航栏:

//You can change the bar style
navigationController?.navigationBar.barStyle = UIBarStyle.Black

// You can change the background color 
navigationController?.navigationBar.barTintColor = UIColor(red: 4 / 255, green: 47 / 255, blue: 66 / 255, alpha: 1)

// You can add a logo on it
let navBarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
navBarImageView.contentMode = .ScaleAspectFit
let navBarImage = UIImage(named: "myNavBarLogo.png")
navBarImageView.image = navBarImage
navigationItem.titleView = navBarImageView

//You can change the text color
 navigationController?.navigationBar.tintColor = UIColor.yellowColor()

//You can change the font
if let font = UIFont(name: "AppleSDGothicNeo-Thin", size: 34) {
    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}

您只需使用这些属性即可获得所需的导航栏。

希望对你有帮助!

您可以在 Interface Builder 中更改导航栏。

您可以通过更改条形色调来更改背景颜色。您还可以更改标题字体、颜色等,如下所示: