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

标签 xcode swift ios8 navbar

我到处查看并测试了 Stack 上发布的所有代码片段,但没有任何效果适合我,因为我需要它工作。

我只想设置:

  • 导航栏高度
  • 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

最佳答案

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

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

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

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

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

Swift 中的解决方案:

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

  • 标题和项目没有放在导航栏的中间
  • 当用户在 View Controller 之间导航时,标题和项目会闪烁

您需要先对 UINavigationBar 进行子类化,然后在 Storyboard中选择导航栏元素,然后在 “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;
    }
} 

还有我的要点:https://gist.github.com/pai911/8fa123d4068b61ad0ff7

iOS 10 更新:

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

iOS 10 custom navigation bar height

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

关于xcode - iOS 8 Swift Xcode 6 - 设置顶部导航栏 bg 颜色和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28705442/

相关文章:

objective-c - 如何存储类变量,然后在 Objective-C 中调用该类的静态方法

ios - 以编程方式在 Swift 中添加 UIBarButtonItem 操作

ios - 如何在 iOS 15 及更早版本上支持程序化 View 关闭

ios - Swift:如何切换两个 UI 开关

ios - WebView Xcode 7 空白错误

ios - Xcode 构建未显示在 iTunes Connect 上

ios - 呈现一个导航栏和一个非 Root View 选项卡 Controller

ios - 具有动态按钮列表的操作表类型的 UIAlertViewController

ios - 滚动后的单元格在图像上显示图像

iOS 8 – 创建临时的 NSManagedObjects