Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3: How to set up UINavigationBar setBackgroundImage in AppDelegate

Tags:

ios

swift

swift3

I recently migrated from Swift 2 to Swift 3 and I'm stuck on one part. In my AppDelegate, I'd set up the default settings for the UINavigationBar as coded below. The challenge I'm having is that the setBackgroundImage is no longer recognized in Swift 3 and I can't find an alternative.

Has anyone had the same issue happen and been able to resolve it?

    let turquoiseBackgroundImage:UIImage = UIImage(named: "navigationBarTurqoise")!


    **// TODO Below is not working with Swift 3
    UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, forBarPosition: .Default)**


    // Set up Back button to be white in Navigation bar
    UINavigationBar.appearance().barStyle = UIBarStyle.default
    UINavigationBar.appearance().tintColor = UIColor.white

    // Font Name and Size of Title in Navigation Bar
    if let font = UIFont(name: "TitilliumWeb-Light", size: 20) {
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : font, NSForegroundColorAttributeName : UIColor.white]
    }

    // Remove hairline between navigation bar and anything below such as search bar
    UINavigationBar.appearance().shadowImage = UIImage()

    // Set up status bar (battery, time, etc.) to white
    UIApplication.shared.statusBarStyle = .lightContent
like image 942
Azam Khan Avatar asked Dec 08 '25 04:12

Azam Khan


1 Answers

UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, forBarPosition: .Default)

rewrite code like this

UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, for: .default)
like image 119
Azhong Avatar answered Dec 10 '25 19:12

Azhong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!