下面的代碼不適用於我,任何人都可以幫助弄清楚什麼是錯誤的?在swift中設置navigationBar背景圖像
var image = UIImage(named: "10384605_10152519403846670_5189785375955620548_n.jpg") as UIImage
self.navigationController.navigationBar.setBackgroundImage(image , forBarMetrics:UIBarMetrics)
下面的代碼不適用於我,任何人都可以幫助弄清楚什麼是錯誤的?在swift中設置navigationBar背景圖像
var image = UIImage(named: "10384605_10152519403846670_5189785375955620548_n.jpg") as UIImage
self.navigationController.navigationBar.setBackgroundImage(image , forBarMetrics:UIBarMetrics)
self.navigationController.navigationBar.setBackgroundImage(image,
forBarMetrics: .Default)
let navBackgroundImage:UIImage! = UIImage(named: "navbar_bg")
[UINavigationBar .appearance().setBackgroundImage(navBackgroundImage, forBarMetrics:.Default)]
只有代碼答案往往被視爲低質量。你可以編輯你的答案,並添加一個解釋如何解決這個問題? – Dijkgraaf
在AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Image Background Navigation Bar
let navBackgroundImage:UIImage! = UIImage(named: "backgroundNB.png")
UINavigationBar.appearance().setBackgroundImage(navBackgroundImage, forBarMetrics: .Default)
return true
}
下面的代碼添加到didFinishLaunchingWithOptions方法在AppDelegate.swift:
if let myImage = UIImage(named: "navBarImage.jpg"){
UINavigationBar.appearance().setBackgroundImage(myImage, for: .default)
}
在夫特3:
如果你想在後臺添加一個重複出現的圖像,你可以撥打這個電話中的AppDelegate> didFinishLaunchingWithOptions:
let image = UIImage(named: "imageNameInAsset")
UINavigationBar.appearance().setBackgroundImage(image, for: .default)
如果你想將圖像添加到您需要做的導航欄的中心這在的ViewController> viewWillAppear中:
let titleView = UIImageView(image: UIImage(named: "imageNameInAsset"))
self.navigationItem.titleView = titleView
對於夫特3:
在AppDelega te.swift:
UINavigationBar.appearance().setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
OR
在viewDidLoad中():
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
如果你要填寫導航欄中的圖像只使用代碼:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your_Background_Image_Name")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
@benLIVE問如何d o後退按鈕,當我找到接受的答案時,我正在做的事情,所以我想我也會在這裏留下這個(如果有點晚)b/c如果你要更換導航欄圖標,你可以更換他們全部!
let cubeIcon = UIImageView(image: yourImage)
cubeIcon.contentMode = .scaleAspectFit
self.navigationItem.backBarButtonItem?.image = cubeIcon.image
如何設置後退按鈕的圖像? – benLIVE
謝謝,這工作得很好! – agrippa