iOS 8 Swift Xcode 6 - 设置顶部导航栏bg颜色和高度 [英] iOS 8 Swift Xcode 6 - Set top nav bar bg color and height

查看:252
本文介绍了iOS 8 Swift Xcode 6 - 设置顶部导航栏bg颜色和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我只是想设置:


  • 导航栏高度

  • 导航栏RGB颜色RGB

  • 导航栏居中徽标

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

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

非常感谢您的回答!

这是我在ViewController.swift中的代码

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


推荐答案

导航栏高度:

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

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 
}

RGB中的导航栏bg颜色:

Nav bar bg color in RGB:

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

In a custom navigation controller subclass...

func viewDidLoad() {
    super.viewDidLoad()

    navigationBar.barTintColor = // YOUR COLOR
}

或使用外观代理

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

导航栏居中徽标

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

In a custom view controller...

func viewDidLoad() {
    super.viewDidLoad()

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

这篇关于iOS 8 Swift Xcode 6 - 设置顶部导航栏bg颜色和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆