swift: navigation bar 設定背景圖片

2017062812:19


# 1

nav_bg.png  320x44 (@x1)

//
//  ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
       //nav_bg.png  320x44
        navigationController?.navigationBar.setBackgroundImage(UIImage(named: "nav_bg.png"), for: .default)
        
        //讓nav bar下方的灰色線隱藏
        navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


結果如下
status bar部分有點 !@#$$$.....



 




# 2

//
//  ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //修改 status bar 背景色
        UIApplication.shared.statusBarView?.backgroundColor = UIColor(red:0.95, green:0.56, blue:0.61, alpha:1.0)
        
        navigationController?.navigationBar.setBackgroundImage(UIImage(named: "nav_bg.png"), for: .default)
        
        //讓nav bar下方的灰色線隱藏 https://stackoverflow.com/questions/19226965/how-to-hide-ios7-uinavigationbar-1px-bottom-line
        navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


extension UIApplication {
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}


 


結果如下





#3

另外也可將 背景圖規格設計為 320x66 (@x1)
就不需另外設定 status bar顏色
例如

//
//  ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
       //nav_bg.png  320x66
        navigationController?.navigationBar.setBackgroundImage(UIImage(named: "nav_bg2.png"), for: .default)
        
        //讓nav bar下方的灰色線隱藏
        navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}








另外 RGB 的轉換為 UIColor()
http://uicolor.xyz/

 


  •    (悄悄話) 2F
  • <悄悄話留言,不公開>
  •   電腦工坊-小郭 於 2017-07-01 21:29 1F
  • 搞IPHONE  OS歐XD

  • 版主於 2017-07-02 06:49 回覆
    學學 app 設計