NavigationBar background image for iOS 10 XCode 8.1 Swift 3.0

给你一囗甜甜゛ 提交于 2019-12-09 19:13:30

问题


How to set the background image for this specific settings? iOS10 and XCode8.1? I tried everything below:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "navigationBg")!.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default)
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "navigationBg")! , for: .any, barMetrics: .default)
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "navigationBg")! , for: .default)

I'm able to set the color of navigationBar using storyboard but unable to set the image.


回答1:


Trying setting "self.navigationItem.titleView" property

Reference : how-to-put-an-image-as-the-navigation-bar-title

Update:

Try this, instead of setting in UINavigationBar()

let navBackgroundImage:UIImage! = UIImage(named: "<navigationBarImage>")
self.navigationController?.navigationBar.setBackgroundImage(navBackgroundImage,
                                                            for: .default)



回答2:


Here is the code which perfectly work for me in Swift 3.
Put this in didFinishLaunchingWithOptions.

let image = bg_banner // let image = #imageLiteral(resourceName: "bg_banner")
UINavigationBar.appearance().setBackgroundImage(image.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch), for: .default)

And here is the output which I received.




回答3:


To set NavigationBar Background Image you can write this:

 self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"pattern.png"),
                                                                for: .default)



回答4:


For setting up the background image to whole navigation bar use this cde in swift 3.0,

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "Exam-104")!.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0)), for: .default)

Hope this will help you.




回答5:


XCode 8.1 Swift 3.0 iOS 10

from Apple documentation:

You can set a custom background image for your navigation bar. You can do this using setBackgroundImage:forBarMetrics:. Note that you must specify bar metrics because navigation bars have different dimensions on different devices and orientations.

let image = UIImage(named: "navigationBg")!
if let navigationController = self.navigationController {
   navigationController.navigationBar.setBackgroundImage(image, for: .default)
}

Reference: Navigation Bars



来源:https://stackoverflow.com/questions/40829692/navigationbar-background-image-for-ios-10-xcode-8-1-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!