-
-
Save kamwing/0040a8dd5cf03c021d288eefe08b6dcf to your computer and use it in GitHub Desktop.
Custom UINavigationBar Height
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
// MARK: - SceneDelegate | |
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
let navigationController = UINavigationController(navigationBarClass: NavigationBar.self, toolbarClass: nil) | |
(navigationController.navigationBar as! NavigationBar).preferredHeight = 88 | |
navigationController.setViewControllers([ViewController()], animated: false) | |
guard let windowScene = (scene as? UIWindowScene) else { return } | |
let window = UIWindow(windowScene: windowScene) | |
window.rootViewController = navigationController | |
window.makeKeyAndVisible() | |
self.window = window | |
} | |
} | |
// MARK: - NavigationBar | |
class NavigationBar: UINavigationBar { | |
var preferredHeight: CGFloat = 44 | |
override var frame: CGRect { | |
get { | |
return super.frame | |
} set { | |
var frame = newValue | |
frame.size.height = preferredHeight | |
super.frame = frame | |
} | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: preferredHeight) | |
} | |
} | |
// MARK: - ViewController | |
class ViewController: UITableViewController { | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
transitionCoordinator?.animate { _ in | |
(self.navigationController?.navigationBar as? NavigationBar)?.preferredHeight = 88 | |
} | |
} | |
} | |
// MARK: - DetailViewController | |
class DetailViewController: UITableViewController { | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
transitionCoordinator?.animate { _ in | |
(self.navigationController?.navigationBar as? NavigationBar)?.preferredHeight = 44 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment