0

Is there any way to split navigation bar into two parts or need to add any custom view, to add two colors in navigation bar like below-

If I put below code-

It applied on whole navigation bar, Not only left corner.

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

enter image description here

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
iDeveloper
  • 2,339
  • 2
  • 24
  • 38

4 Answers4

3

Sure, design your two-color image beforehand or draw it in code and call setBackgroundImage(_:for:barMetrics:).

https://developer.apple.com/documentation/uikit/uinavigationbar/1624968-setbackgroundimage

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

You have to change the background image of navigation bar like this:

if condition {
    navigationController?.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "bg1"), for: .default)
} else {
    navigationController?.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "bg2"), for: .default)
}

where bg1 is: bg1

and bg 2 is: bg2

and the result is:

Result

Keep in mind:

  • You can color your image in code if you want more dynamic range of colors.
  • You can use multiple imageViews behind navigation bar and set colors un them. (One for left part and one for right). And hide the navigation bar background image. So the illusion looks same.
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
0

You can draw required gradient (OR two distinct non-gradient color) image with swift code first and then set it to the header.

Creating an Image with an Image Renderer

Use the image(actions:) method to create an image (UIImage object) with an image renderer. https://developer.apple.com/documentation/uikit/uigraphicsimagerenderer https://developer.apple.com/library/archive/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/BezierPaths/BezierPaths.html

https://stackoverflow.com/a/30761714/8238512

To set created image above you already know

Use the above generated image to set in navigationBar

self.navigationController?.navigationBar.setBackgroundImage(< *useAboveGeneratedImage* >),for:.default)
Rizwan
  • 3,324
  • 3
  • 17
  • 38
0

I would suggest to create a custom navigation bar. Make a UIView and add some delegates for back buttons, hamburger menu (if required).

vinbhai4u
  • 1,329
  • 3
  • 19
  • 36