iOS 8 Swift Xcode 6 - 设置顶部导航栏bg颜色和高度

Eri*_*ker 7 xcode navbar swift ios8

我到处查看并测试了Stack上发布的所有代码片段,但是对我来说没什么用,因为我需要它才能工作.

我只是想设置:

  • 导航栏高度
  • 导航条RGB RGB中的颜色
  • 导航栏居中徽标

我正在使用iOS8,Xcode 6和Swift.

非常感谢您的回答!

这是我在ViewController.swift中的代码

// Set nav bar height

    navigationController?.navigationBar.frame.origin.y = -10

    // Set nav bar bg color

    var navBarColor = UIColor(red: 4 / 255, green: 47 / 255, blue: 66 / 255, alpha: 1)

    navigationController?.navigationBar.barTintColor = navBarColor

    // Set nav bar logo

    let navBarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))

    navBarImageView.contentMode = .ScaleAspectFit

    let navBarImage = UIImage(named: "navBarLogo.png")

    navBarImageView.image = navBarImage

    navigationItem.titleView = navBarImageView
Run Code Online (Sandbox Code Playgroud)

Joh*_*ler 12

在接受的答案中应用代码后,高度似乎根本没有变化.

这不是一件容易的事......我在线调查了几篇文章(其中大部分都是在Objective-C中).

最有用的是:http://www.emdentec.com/blog/2014/2/25/hacking-uinavigationbar

但它的最终解决方案并没有把项目放在中间,而是在Swift中.

所以我在Swift中提出了一个可行的版本.希望它可以帮助一些人,因为我被拯救了这么多宝贵的时间.

Swift中的解决方案:

以下代码将解决您可能遇到的一些问题:

  • 标题和项目不会放在导航栏的中间
  • 当用户在视图控制器之间导航时,标题和项目将弹出

您需要首先对UINavigationBar进行子类化,然后在故事板中选择导航栏元素,并在"Identity Inspector"选项卡中将新类设置为自定义类

import UIKit

class UINavigationBarTaller: UINavigationBar {
    ///The height you want your navigation bar to be of
    static let navigationBarHeight: CGFloat = 64

    ///The difference between new height and default height
    static let heightIncrease:CGFloat = navigationBarHeight - 44

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }

    private func initialize() {
        let shift = UINavigationBarTaller.heightIncrease/2

        ///Transform all view to shift upward for [shift] point
        self.transform =
            CGAffineTransformMakeTranslation(0, -shift)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let shift = UINavigationBarTaller.heightIncrease/2

        ///Move the background down for [shift] point
        let classNamesToReposition: [String] = ["_UINavigationBarBackground"]
        for view: UIView in self.subviews {
            if classNamesToReposition.contains(NSStringFromClass(view.dynamicType)) {
                let bounds: CGRect = self.bounds
                var frame: CGRect = view.frame
                frame.origin.y = bounds.origin.y + shift - 20.0
                frame.size.height = bounds.size.height + 20.0
                view.frame = frame
            }
        }
    }

    override func sizeThatFits(size: CGSize) -> CGSize {
        let amendedSize:CGSize = super.sizeThatFits(size)
        let newSize:CGSize = CGSizeMake(amendedSize.width, UINavigationBarTaller.navigationBarHeight);
        return newSize;
    }
} 
Run Code Online (Sandbox Code Playgroud)

另外依据我的要点:https://gist.github.com/pai911/8fa123d4068b61ad0ff7

iOS 10更新:

不幸的是,这个代码在iOS 10中断了,有人帮助修复它,在这里你去:

iOS 10自定义导航栏高度

需要说明的是,这段代码有点hacky,因为它依赖于导航栏的内部结构......所以如果你决定使用它,那么请为即将发生的任何可能破坏此代码的更改做好准备......


DBo*_*yer 4

导航栏高度:

在自定义导航控制器子类中...

这个的技巧是不改变导航栏的实际高度,而是调整其原点。

func viewDidLoad() {
    super.viewDidLoad()

    navigationBar.frame.origin.y = -10 
}
Run Code Online (Sandbox Code Playgroud)

RGB 中的导航栏背景颜色:

在自定义导航控制器子类中...

func viewDidLoad() {
    super.viewDidLoad()

    navigationBar.barTintColor = // YOUR COLOR
}
Run Code Online (Sandbox Code Playgroud)

或使用外观代理

UINavigationBar.appearance().barTintColor = // YOUR COLOR
Run Code Online (Sandbox Code Playgroud)

导航栏居中徽标

在自定义视图控制器中...

func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.titleView = UIImageView(image: // YOUR LOGO)
}
Run Code Online (Sandbox Code Playgroud)