简体   繁体   中英

UINavigationBar forces title view to center on back

I have implemented a custom title view for my navigation bar, containing an image and a label. This view is aligned to the left of the bar, by setting its origin.x to 10 pixels.

So far so good, but when I push a view controller and then go back to the main screen, the title view is centered instead of positioned on the left.

I tried setting its left and back bar button items to nil and its origin.x to 10, but nothing seems to work. I tried both viewWillAppear and viewDidAppear:

CGRect frame = self.navigationItem.titleView.frame;
frame.origin.x = 10;
self.navigationItem.titleView.frame = frame;

self.navigationItem.backBarButtonItem = nil;
self.navigationItem.leftBarButtonItem = nil;

How can I accomplish this?

First off you should set up the navigationItem in viewDidLoad (after calling [super viewDidLoad]) NOT viewWill/DidAppear.

Are you only setting up the navigationItem on the second view controller and not the main vc???

If so you need to set the navigationItem on both, or alternatively in a Base View Controller class which both view controllers should subclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM