PGDev
PGDev

Reputation: 24341

Background image of custom UINavigationBar - Swift 3.0

I want to set the background image of a custom UINavigationBar. I googled and found many solutions related to the issue. But still, I am not able to solve the problem.

Here is the image I want to set as background of custom UINavigationBar:

enter image description here

The code that I am using to set the background image:

import UIKit

class CustomNavigationBarViewController: UIViewController
{
    @IBOutlet weak var customNavigationBar: UINavigationBar!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.customNavigationBar.setBackgroundImage(#imageLiteral(resourceName: "TopGradient"), for: .default)
    }

    override var prefersStatusBarHidden: Bool
    {
        return true
    }
}

The screenshot of the result that I am getting:

enter image description here

Instead of the image, I am only getting a gray colored background of UINavigationBar.

Upvotes: 1

Views: 166

Answers (1)

iBug
iBug

Reputation: 2352

You should resize your image. UINavigationBar has default height of 64 px. Your image height is more than that.

Check this with correct image size. I have resized the image you were using.

enter image description here

Hope this will help.

Upvotes: 1

Related Questions