2

I'm programmatically creating a UINavigationController:

navigationController = UINavigationController(rootViewController: modeSelectVC!)
navigationController?.delegate = self

window.rootViewController = navigationController
window.makeKeyAndVisible()

Then I customize my UINavigationBar appearance like so:

navigationBarAppereance.barTintColor = UIColor.ColorPalette.bostonUniversityRed
navigationBarAppereance.tintColor = UIColor.ColorPalette.eerieBlack
navigationBarAppereance.barStyle = .black
navigationBarAppereance.isTranslucent = true
navigationBarAppereance.prefersLargeTitles = true
navigationBarAppereance.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.ColorPalette.babyPowder]

The configured UINavigationBar is hidden in the rootViewController provided along with the status bar but when the rootViewController pushes another view controller, both the status bar and the navigation bar becomes visible like so:

UINavigationController with problematic <code>Back</code> button placement

The result is as I programmed it to be but as you can also see above, the back button is misaligned. If I let go of the large title preferring, then the back button is properly aligned as usual but why does it get misaligned when large title is used.

Can
  • 4,516
  • 6
  • 28
  • 50

2 Answers2

2

There is a good answer for this here:

You should pin the top of the navigation bar to the bottom of the status bar and use the UIBarPositioningDelegate mechanism to set its position to .topAttached, which will cause it to stretch under the status bar correctly.

Thomas Wang
  • 2,233
  • 2
  • 14
  • 24
  • I added this: `func position(for bar: UIBarPositioning) -> UIBarPosition { return .topAttached }` but sadly the problem is still ongoing. – Can Oct 13 '17 at 10:51
  • @CanSürmeli maybe try `navigationController.navigationBar.clipsToBounds = false`? – Thomas Wang Oct 14 '17 at 00:30
  • Unfortunately, no again. – Can Oct 14 '17 at 08:38
  • @CanSürmeli what about `navigationController?.navigationBar.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)`? – Thomas Wang Oct 14 '17 at 12:18
  • I get the error: `Value of type `UINavigationBar` has no member `anchor``. And frankly, I don't think or want to set/modify UINavigationBar's placement as it's automatically aligned. Otherwise, I feel it may break something. – Can Oct 16 '17 at 08:44
  • @CanSürmeli yea, understandable. i'll try and look into this a bit more and get back to you when i have a solution! – Thomas Wang Oct 16 '17 at 19:47
2

I kind of solved the problem. Once I enabled the status bar on the initial view controller, afterwards, whatever got presented, correctly displayed the navigation bar along with the status bar.

I still don't know what caused the issue(if it was an implementation mistake of mine or poor documentation) but dealing with the UINavigationBar can be tricky!

Can
  • 4,516
  • 6
  • 28
  • 50