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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28705442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:43:20  来源:igfitidea点击:

iOS 8 Swift Xcode 6 - Set top nav bar bg color and height

xcodeswiftios8navbar

提问by Erik Lydecker

I have looked everywhere and tested all the code snippets posted on Stack, but nothing works for me as I need it to work.

我到处查看并测试了 Stack 上发布的所有代码片段,但没有任何东西对我有用,因为我需要它才能工作。

I simply want to set:

我只想设置:

  • Nav bar height
  • Nav bar bg color in RGB
  • Nav bar centered logo
  • 导航栏高度
  • RGB 中的导航栏 bg 颜色
  • 导航栏居中徽标

I'm working with iOS8, Xcode 6 and Swift.

我正在使用 iOS8、Xcode 6 和 Swift。

Many thanks for a clear answer!

非常感谢您的明确答复!

This is my code in ViewController.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

采纳答案by DBoyer

Nav bar height:

导航栏高度:

In a custom navigation controller subclass...

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

The trick with this one is to NOT change the actual height of the navigation bar and instead adjust its origin.

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

func viewDidLoad() {
    super.viewDidLoad()

    navigationBar.frame.origin.y = -10 
}

Nav bar bg color in RGB:

RGB 中的导航栏 bg 颜色:

In a custom navigation controller subclass...

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

func viewDidLoad() {
    super.viewDidLoad()

    navigationBar.barTintColor = // YOUR COLOR
}

or use the appearance proxy

或使用外观代理

UINavigationBar.appearance().barTintColor = // YOUR COLOR

Nav bar centered logo

导航栏居中徽标

In a custom view controller...

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

func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.titleView = UIImageView(image: // YOUR LOGO)
}

回答by John the Traveler

After applying the code in the accepted answer, the height doesn't seem to change at all..

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

It's not an easy job...and I've surveyed several articles online (most of them in Objective-C).

这不是一件容易的工作……我已经在线调查了几篇文章(其中大部分是 Objective-C 的)。

The most useful one is this: http://www.emdentec.com/blog/2014/2/25/hacking-uinavigationbar

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

But its final solution does not put items in the middle, and it's not in Swift.

但是它的最终解决方案并没有将项目放在中间,而且在 Swift 中也没有。

So I come up with a workable version in Swift.Hope it helps some people as I was saved so many precious time on SO.

所以我想出了一个可行的Swift版本。希望它可以帮助一些人,因为我在 SO 上节省了很多宝贵的时间。

Solution in Swift:

Swift 中的解决方案:

The following code will solve some issues you may have encountered:

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

  • The title & items are not placed in the middle of the navigation bar
  • The title & items would flick when the user navigates between view controllers
  • 标题和项目没有放在导航栏的中间
  • 当用户在视图控制器之间导航时,标题和项目会闪烁

You need to subclass the UINavigationBarfirst, and in your storyboard, select the navigation bar element, and in the "Identity Inspector"tab, set the new class as the Custom Class

您需要先对UINavigationBar进行子类化,然后在您的故事板中,选择导航栏元素,然后在“身份检查器”选项卡中,将新类设置为自定义类

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;
    }
} 

Also on my gist: https://gist.github.com/pai911/8fa123d4068b61ad0ff7

同样在我的要点上:https: //gist.github.com/pai911/8fa123d4068b61ad0ff7

iOS 10 Update:

iOS 10 更新:

Unfortunately, this code breaks in iOS 10, there is someone who helps fix it, here you go:

不幸的是,这段代码在 iOS 10 中被破坏了,有人帮助修复它,你去吧:

iOS 10 custom navigation bar height

iOS 10 自定义导航栏高度

And to be clear, this code is kind of hacky since it depends on the navigation bar's internal structure...so if you decide to use it anyway, be prepared for any upcoming changes that may break this code...

需要明确的是,这段代码有点老套,因为它取决于导航栏的内部结构……所以如果你决定无论如何都要使用它,请准备好迎接可能会破坏这段代码的任何即将发生的变化……

回答by AndreasLukas

Great answer from Bon Bon!

Bon Bon 的精彩回答!

In Swift 3 however make sure you replace

但是在 Swift 3 中,请确保您替换

let classNamesToReposition: [String] = ["_UINavigationBarBackground"]

with

let classNamesToReposition: [ String ] = [ "_UIBarBackground" ]

Otherwise, it wont work.

否则,它不会工作。