Pressing Right Bar Button Item Moves Left Bar Butt

2019-08-05 17:32发布

问题:

I have three left bar button items on my web view nav bar. One that goes back to the the menu of my app and back and forward buttons. I have one right bar button that opens an activity view controller. When I press the right button, the forward and back buttons on the left get moved down and off of the nav bar.

- (void)viewDidLoad
{   
    leftBarButtonItems = [NSArray arrayWithObjects:@"Menu", @"Back", @"Forward", nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *urlAddress = @"http://www.littleheartrecords.com/releases.html";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showMenu)];
    self.navigationItem.rightBarButtonItem = systemAction;

    UIBarButtonItem *openItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStylePlain target:self action:@selector(openButtonPressed)];
    self.navigationItem.leftBarButtonItem = openItem;

    UIImage *back = [UIImage imageNamed:@"back_arrow"];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"" style: UIBarButtonItemStylePlain target:self action:@selector(GoBack)];
    [backButton setImage:back];

    UIImage *forward = [UIImage imageNamed:@"forward_arrow"];
    UIBarButtonItem *forwardButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(GoForward)];
    [forwardButton setImage:forward];

    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:openItem, backButton, forwardButton, nil];
}

回答1:

Strangely, what worked for me was setting the title of the buttons with images to a single space:

barButtonItem.title = @" ";
barButtonItem.image = [UIImage imageNamed:@"MenuIcon.png"];