Skip to content

Instantly share code, notes, and snippets.

@grifas
Last active January 11, 2018 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grifas/219bb4fdabf4cd24c4d9e98b11251336 to your computer and use it in GitHub Desktop.
Save grifas/219bb4fdabf4cd24c4d9e98b11251336 to your computer and use it in GitHub Desktop.
Pushed View Controller with a transparent navigation bar
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
initNavigationBar()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = .lightContent
if isMovingFromParentViewController {
navigationController?.setNavigationBarHidden(false, animated: true)
} else {
// Nothing
}
}
// Initialization
func initNavigationBar() {
UIApplication.shared.statusBarStyle = .default
// 1. hide the existing nav bar
navigationController?.setNavigationBarHidden(true, animated: true)
// 2. create a new nav bar and style it
let newNavBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.width, height: 64))
newNavBar.tintColor = .blue
newNavBar.isTranslucent = true
newNavBar.setBackgroundImage(UIImage(), for: .default)
newNavBar.shadowImage = UIImage()
newNavBar.titleTextAttributes = [
NSAttributedStringKey.foregroundColor: .blue,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17)
]
// 3. add a new navigation item w/title to the new nav bar
let newItem = UINavigationItem(title: title ?? "")
newNavBar.setItems([newItem], animated: true)
// 4. add the nav bar to the main view
self.view.addSubview(newNavBar)
navigationController?.interactivePopGestureRecognizer?.delegate = nil
}
@objc func dismissView() {
navigationController?.popViewController(animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment