Last active
January 11, 2018 15:07
-
-
Save grifas/219bb4fdabf4cd24c4d9e98b11251336 to your computer and use it in GitHub Desktop.
Pushed View Controller with a transparent navigation bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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