Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Last active March 29, 2019 04:54
Show Gist options
  • Select an option

  • Save fuxingloh/e7a569a08b3512e41761 to your computer and use it in GitHub Desktop.

Select an option

Save fuxingloh/e7a569a08b3512e41761 to your computer and use it in GitHub Desktop.
Add fake navigation bar, remove, revert, load, unload and change status tint color, fake navigation bar can be used to assist transition from a controller with navigation background to one without.
extension UIViewController {
// Nav tag id
private var fakeNavTag: Int{get{return 80327}}
// Remove bar bar color and add fake nav bar
public func loadFakeNavigationBar(){
removeNavigationBarBackground()
addFakeNavigationBar()
}
// Add back real nav bar
public func loadRealNavigationBar(){
if let navBar = self.view.viewWithTag(fakeNavTag){
navBar.hidden = true
}
revertNavigationBarBackground()
}
// Add a fake nav bar on top
public func addFakeNavigationBar(){
if let navBar = self.view.viewWithTag(fakeNavTag){
navBar.hidden = false
navBar.frame = CGRectMake(0, 0, self.view.bounds.width, 64)
}else{
let fakeNavBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.bounds.width, 64))
fakeNavBar.tag = fakeNavTag
view.addSubview(fakeNavBar)
}
}
public func addFakeNavigationBarBehide(){
if let navBar = self.view.viewWithTag(fakeNavTag){
navBar.hidden = false
navBar.frame = CGRectMake(0, -64, self.view.bounds.width, 64)
}else{
let fakeNavBar = UINavigationBar(frame: CGRectMake(0, -64, self.view.bounds.width, 64))
fakeNavBar.tag = fakeNavTag
view.addSubview(fakeNavBar)
}
}
// Convert to transparent nav bar background color
public func removeNavigationBarBackground(){
self.navigationController!.navigationBar.translucent = true
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController!.navigationBar.shadowImage = UIImage()
}
// Revert to old nav bar background color
public func revertNavigationBarBackground(){
self.navigationController!.navigationBar.translucent = false
self.navigationController!.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
self.navigationController!.navigationBar.shadowImage = nil
}
public func setBlackStatusBar(){
UIApplication.sharedApplication().setStatusBarStyle(.Default, animated: true)
}
public func setWhiteStatusBar(){
UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: true)
}
public func setBlackStatusNavBar() {
UIApplication.sharedApplication().setStatusBarStyle(.Default, animated: true)
navigationController!.navigationBar.tintColor = UIColor.blackColor()
}
public func setWhiteStatusNavBar() {
UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: true)
navigationController!.navigationBar.tintColor = UIColor.whiteColor()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment