Swift: Get width of titleView in UINavigationBar

905 views Asked by At

I created a UIView which I would like to add to the NavigationBar's titleView, like so:

let progressBar = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 5))
progressBar.backgroundColor = UIColor.greenColor()
self.navigationItem.titleView = progressBar

I set the width of the UIView same as the screen's frame to make sure that it is wide enough (see first line of the code above). Then I added the UIView to the NavigationBar (see line 3).

Since the available space within NavigationBar is smaller than self.view.frame.width, my UIView (progressBar) gets shrunk down to fit the width of the NavigationBar. Hence, the width of my UIView (progressBar) is now smaller than self.view.frame.width.

How can I get the new size of the UIView within the NavigationBar?

Thanks a lot in advance!

0

There are 0 answers