14

I'm making an iOS app for iPhone, and I'm using a navigation controller. At some point during the navigation, I'm adding a UISegmentedControl to a view controller, just under the navigation bar from the navigation controller. I'm inserting new background and shadow images in the navigation bar, to make the UISegmentedControl appear as part of the navigation bar. I do it like this:

    // nav bar color image
    let rect = CGRectMake(0, 0, view.frame.width, 0.5)          // Used in navBar, size dosn't matter
    UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
    barBackgroundColor.setFill()
    UIRectFill(rect)
    let navBarBackground = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // setup navbar
    navigationController!.navigationBar.setBackgroundImage(navBarBackground, forBarMetrics: .Default)
    navigationController!.navigationBar.shadowImage = UIImage()
    navigationController!.navigationBar.tintColor = UIColor.blackColor()
    navigationController!.navigationBar.translucent = false

When I navigate away from that given view controller, the navigation bars background is still changed.

How can I restore the navigation bars appearance?

Or...

Is there another way embed the UISegmentedControl into an expanded navigation bar?

Image of navigation bar with custom background and Segmented Control below: Navbar with custom background. plus segment control below

When navigating back, the navigation bar cuntinues to be custom: enter image description here

EDIT:

In a view controller before i change the background images, i try to safe the standart image:

override func viewDidAppear(animated: Bool) {

    if sharedVariables.standartNavBarBackgroundImage == nil {
        let herp = navigationController!.navigationBar.backgroundImageForBarMetrics(.Default)
        sharedVariables.standartNavBarBackgroundImage = herp
        let derp = navigationController!.navigationBar.shadowImage
        sharedVariables.standartNavBarShadowImage = derp
    }
}

Both herp and derp are nil after being set, dispite the navigationbar is visible at this momont. How come?

2
  • Show a picture of the end result you're looking for and what it looks like if you don't change the images
    – Wain
    Dec 7, 2015 at 19:41
  • Images added..The end result should be the standart navigation bar appearance..
    – Wiingaard
    Dec 7, 2015 at 20:18

2 Answers 2

39

You should be able to get the default appearance back just by setting the background image and shadow image to nil.

4

With iOS 11.* and Swift 4, you need to set the barTintColor to nil.

navigationController?.navigationBar.barTintColor = nil
1
  • 1
    Not if it hasn't been changed before.
    – Daniel
    Apr 8, 2018 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.