Search code examples
iosautolayoutuinavigationbartitleview

Maximize Width of TitleView


I'm using a storyboard and added a UIView to a navigation item with 2 sub UILabels. I want the UIView to be the maximum width possible. I noticed that I cannot use auto-layout to create constraints to do this. This means I get results like this.

enter image description here

Is there a way I can have the titleview automatically resize to fill the navigation bar?


Solution

  • I ended up creating the titleview in code. First I set the width to the width of the view. Then I set it to autoresizing flexible width.

    I set this view as the title view. The navigationbar then automatically resizes the view to the correct size.

    I also used the viewWillAppear to set the alpha of the view to 0 and viewDidAppear to fade the view in by setting the alpha to 1. Otherwise you can see the view jump as it gets resized.