Skip to content

Instantly share code, notes, and snippets.

@sebjvidal
Created June 27, 2023 18:14
Show Gist options
  • Save sebjvidal/2f2b6500a6c59a7add2d77631d3511a8 to your computer and use it in GitHub Desktop.
Save sebjvidal/2f2b6500a6c59a7add2d77631d3511a8 to your computer and use it in GitHub Desktop.
Custom UINavigationBar Height
// 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