Monday, 29 October 2018

Header of viewcontroller doesn't fit

So the original setup of the app that I inherited has the navigation bar set like so (this is in my AppDelegate):

private func configureNavigationController(_ navigationController: UINavigationController) {
    navigationController.navigationBar.isTranslucent = false

    self.window?.rootViewController = navigationController

    let imageView = UIImageView(image: UIImage(named: "logo-white"))
    imageView.contentMode = UIViewContentMode.scaleAspectFit

    let center = (navigationController.topViewController?.view.frame.width)! / 2.0 - 44

    let titleView = UIView(frame: CGRect(x: center, y: 0, width: 88, height: 44))
    imageView.frame = titleView.bounds
    titleView.addSubview(imageView)


    UINavigationBar.appearance().barTintColor = UIColor.navBackground
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().addSubview(titleView)
}

This creates the nav bar across every view controller correctly with the image in the center, however I have some new functionality that needs to be on top of everything, and this logo file - logo-white - is still showing up over top.

That's the real problem I want to solve - so if my attempted solution below is wrong, let me know and tell me the correct way.

Anyway, I tried commenting out the code above in my AppDelegate, and putting it in the specific viewcontrollers that I need it for

override func viewDidLoad() {
    super.viewDidLoad()

    let imageView = UIImageView(image: UIImage(named: "logo-white"))
    imageView.contentMode = UIViewContentMode.scaleAspectFit

    let center = (navigationController!.topViewController?.view.frame.width)!// / 2.0 - 44

    let titleView = UIView(frame: CGRect(x: center, y: 0, width: 88, height: 44))

    imageView.frame = titleView.bounds
    titleView.addSubview(imageView)

    navigationItem.titleView = imageView

However this doesn't work - I can either get the logo to show up on the left side of the screen, or slightly off of center, but never center.

I am guessing that this is because the bar has a back button and a little settings icon as well on either side.

So, how do I do this correctly?

Is there a way to make it so that something can cover the logo? Is the solution to move it into my individual view controllers?



from Header of viewcontroller doesn't fit

No comments:

Post a Comment