Configure UINavigationBar globally?

I am trying to change instances UINavigationBarglobally using the code below, which is located in DidFinishLaunchingWithOptions(:_);

let navBarApp = UINavigationBar.appereance()
navBarApp.barTintColor = UIColor.mmtRed

but the result;

figure1

as you can see in the picture, the colors are different, which should be the same. (The color of the buttons is the one I want my navigation bar to have.)

When I add the code as follows:

navBarApp.isTranslucent = false

result;

figure2

The colors are the same right now, but there is a strange gap between UINavigationBarand mainView. So how can I solve this? Any suggestions?

EDIT:

Forgot to mention that I'm using the PageMenu library , maybe this has some effect?

+4
source share
2 answers

automaticallyAdjustsScrollViewInsets viewDidLoad() viewController

override func viewDidLoad() {
    automaticallyAdjustsScrollViewInsets = false
}

, ""

enter image description here

,

:

, backGroundImage , , @WilsonXJ.

extension UIImage {
   static func imageWithColor(tintColor: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        tintColor.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

navBarApp.setBackgroundImage(UIImage.imageWithColor(tintColor: <Custom color>), for: .default)
+1

:

let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
UIColor.redColor().setFill()// your color
UIRectFill(rect)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let navBar = UINavigationBar.appearance()
navBar.setBackgroundImage(image, forBarMetrics: .Default)

0

Source: https://habr.com/ru/post/1662143/


All Articles