zenpain zenpain - 6 months ago 32
Swift Question

How to undo hairline remove in Swift for different pages?

I am developing an app in which the home page of the app has a white navigation bar with the hairline hidden, and the remaining pages having a green nav bar. What I've done so far is put the code for the home page nav bar in its own .swift file, but what happens is that when I navigate to my other pages through the buttons on the home page (as opposed to the menu bar) the navigation bar's still set to white. I think it's because the code that removes the hairline carries through to other pages when accessed through the buttons.

This is what my page should ideally look like:
Homepage & Module

Instead this is what the module page looks like: Module Page Now

This is the code I'm using - wondering if anyone can help me figure out how to reverse the code or if there's an alternate solution.

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

// Set navigation bar tint / background colour
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()

// Set Navigation bar Title colour
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()]

//Set navigation bar Back button tint colour
self.navigationController?.navigationBar.tintColor = UIColor.blackColor()

//Get Rid of 1px hairline
self.navigationController?.navigationBar.setBackgroundImage(
UIImage(),
forBarPosition: .Any,
barMetrics: .Default)

self.navigationController?.navigationBar.shadowImage = UIImage()

}

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)

// Set navigation bar tint / background colour
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0/255, green: 127/255, blue: 106/255, alpha: 1)

// Set Navigation bar Title colour
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

//Set navigation bar Back button tint colour
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()

//Reverse get Rid of 1px hairline code
/*self.navigationController?.navigationBar.setBackgroundImage(
UIImage(),
forBarPosition: .Any,
barMetrics: .Default)

self.navigationController?.navigationBar.shadowImage = UIImage()*/
}

Answer

Was able to solve this by just reversing the code in the viewWillDisappear method.

  //Reverse get Rid of 1px hairline code
self.navigationController?.navigationBar.setBackgroundImage(nil, forBarMetrics: .Default)
Comments