# 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/