简体   繁体   中英

How do I stop leftBarButtonItem from disappearing when navigationItem.titleView and rightBarButtonItem take up most of the UINavigationBar?

I have a leftBarButtonItem (using a custom view) that's acting as a replacement for a back button (as custom views can't be used on backBarButtonItem ). I've also got a rightBarButtonItem (also using a custom view) and a custom titleView.

Everything works fine and dandy when the leftBarButtonItem is smaller than a certain width. The rightBarButtonItem can be huge and the titleView will resize and truncate to fit the available space, which is exactly how it should be, and how I want it.

However, with the same dimensions reversed ( rightBarButtonItem smaller than a certain width and leftBarButtonItem huge), the leftBarButtonItem disappears entirely, and the titleView stretches to fit the entire space that previously would have held both the leftBarButtonItem and the titleView .

I tried swapping the custom views to see if it was something I had done, but the same problem persisted with the views swapped, again only on the leftBarButtonItem . I also tried using a regular title, without the custom titleView , to no avail.

In the documentation for UINavigationItem , under leftBarButtonItems , it notes that "If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the right side of the bar are not displayed." However, there should be more than enough room to fit both buttons and the title, as demonstrated by having a huge rightBarButtonItem and a truncated title. Not to mention, there is only a single leftBarButtonItem , not multiple, and I need it to appear to replace the back button functionality.

Any ideas on how to stop this from happening? Here's a snippet of the code I'm using:

self.navigationItem.titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, 220, 44)];
self.navigationItem.titleView.backgroundColor=[UIColor clearColor];

FollowButton *follow = [[FollowButton alloc] initWithFrame:CGRectMake(0, 0, 62, 34)];
UIBarButtonItem *followButton = [[UIBarButtonItem alloc] initWithCustomView:follow];
self.navigationItem.rightBarButtonItem = followButton;

BackButton *back = [[BackButton alloc] initWithFrame:CGRectMake(0, 0, 108, 34)];
[back addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.leftBarButtonItem = backButton;

使BackButton抗压性优先级更高

Can you check for the titlebar buttons width? Does it actually shrink or does the x-position just change and it gets pushed off the screen.... If the width is actually becoming "0" than you should be able to force set it back to the desired width after the loading of the other buttons is done.

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