以下代码对我不起作用,任何人都可以帮忙弄清楚出了什么问题吗?
var image = UIImage(named: "10384605_10152519403846670_5189785375955620548_n.jpg") as UIImage
self.navigationController.navigationBar.setBackgroundImage(image , forBarMetrics:UIBarMetrics)
答案 0 :(得分:33)
self.navigationController.navigationBar.setBackgroundImage(image,
forBarMetrics: .Default)
答案 1 :(得分:16)
在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
}
答案 2 :(得分:14)
在 Swift 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 :(得分:6)
如果您想在导航栏中填充图片,只需使用以下代码:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your_Background_Image_Name")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
答案 4 :(得分:3)
let navBackgroundImage:UIImage! = UIImage(named: "navbar_bg")
[UINavigationBar .appearance().setBackgroundImage(navBackgroundImage, forBarMetrics:.Default)]
答案 5 :(得分:2)
Swift 3 :
在AppDelegate.swift中:
UINavigationBar.appearance().setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
或强>
在viewDidLoad()中:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
答案 6 :(得分:0)
将以下代码添加到AppDelegate.swift中的didFinishLaunchingWithOptions方法:
if let myImage = UIImage(named: "navBarImage.jpg"){
UINavigationBar.appearance().setBackgroundImage(myImage, for: .default)
}
答案 7 :(得分:0)
@benLIVE询问如何做一个后退按钮,当我找到接受的答案时我正在做的事情,所以我想我也会把它留在这里(如果有点晚)b / c如果你要更换导航栏图标,您也可以替换所有这些图标!
let cubeIcon = UIImageView(image: yourImage)
cubeIcon.contentMode = .scaleAspectFit
self.navigationItem.backBarButtonItem?.image = cubeIcon.image
答案 8 :(得分:0)
在导航控制器中设置backgroundImage
self.navigationController?.navigationBar .setBackgroundImage(UIImage(named: "cricket"), for: .default)
答案 9 :(得分:0)
如果您为应用程序隐藏了导航栏,则必须在视图上显示它确实加载或显示为:
override func viewWillAppear(_ animated: Bool) {
print("\n Debugger : View will appear called")
self.navigationController?.isNavigationBarHidden = false
}
如果您希望导航栏仅在特定的视图控制器中显示,则必须通过以下方式使导航栏消失:
override func viewDidDisappear(_ animated: Bool) {
print("\n Debugger : View did disapper called")
self.navigationController?.isNavigationBarHidden = true
}
导航栏设置方法
private func navigationBarSetup(){
print("\n Debugger : Navigation Bar setup method called")
self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: "your image name "), for: .default)
let backButton = UIBarButtonItem(image: UIImage(named: "your Image name"), style: .plain, target: self, action: #selector(Your selector method))
backButton.tintColor = UIColor.white
self.navigationItem.leftBarButtonItem = backButton
let rightButton = UIBarButtonItem(image: UIImage(named: "your Image name"), style: .plain, target: self, action: #selector(Your selector method))
backButton.tintColor = UIColor.white
self.navigationItem.rightBarButtonItem = rightButton
}