I would like to have a custom titleView that fills the useful spaces besides the BarButtonItems.
@property (strong, nonatomic) IBOutlet UIView *testView;
in ViewDidLoad
_testView.frame=CGRectMake(0, 0, self.view.frame.size.width-self.navigationItem.leftBarButtonItem.width - self.view.layoutMargins.right - self.view.layoutMargins.left, 44);
self.navigationItem.titleView =_testView;
But things are not working as I expected:
Several questions:
For iPhone 6S Plus, before assigning _testView to navigationItem.titleView, the width was 398, but after viewDidAppear, it became 329, why is that?
There is a gap between the titleView and the right border, is there any way to delete the gap?
For the pages with only auto-generated back button, how may I get it's width?
Updated: Thanks for @JoBu1324 post, the width of the left barButton can be acquired at viewDidAppear instead of viewDidLoad by:
UIView *leftBarButtonView = [self.navigationItem.leftBarButtonItem valueForKey:@"view"];
CGFloat leftBarButtonWidth = leftBarButtonView ? [leftBarButtonView frame].size.width : (CGFloat)0.0;
Also the left margin can be acquired with leftBarButtonView.frame.origin.x.
What left unclear to me is the right margin(12px in my case) and the gap between leftBarButtonView and titleView, which is 6px in my case.