UIAlertController moves leftBarButtonItem down
Asked Answered
C

2

9

I've created a UIAlertController with the preferred style of UIAlertControllerStyleAlert. The alert shows when the leftBarButtonItem is tapped. I created a UIBarButtonItem property called backButton and set the leftBarButtonItem = self.backButton. This is working as designed. I'm not using storyboards.

The problem is that the leftBarButtonItem moves down (my guess: about 20pts) when the alert shows. Why is this happening?

I know how to show/hide the button so the user can't see that the button when it has moved down. However, that sucks. Why is it happening in the first place?

I haven't found any similar issues online.

@property (strong, nonatomic) IBOutlet UIBarButtonItem *backButton;

in viewDidLoad:

self.backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[self.backButton setImage:[UIImage imageNamed:@"back-arrow-grey"]];
self.navigationItem.leftBarButtonItem = self.backButton;

in backButtonPressed:

{
    self.navigationItem.leftBarButtonItem = nil; //to hide backButton because it moves down
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"My title" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *actionLeave = [UIAlertAction actionWithTitle:@"Leave" style:UIAlertActionStyleDefault handler:...//which works correctly

    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Go back" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        self.navigationItem.leftBarButtonItem = self.backButton; //to show backButton again now that the alert is dismissed
        //other things happen here that work as designed
    }];

    [alertController addAction:actionLeave];
    [alertController addAction:actionCancel];
    [self presentViewController:alertController animated:YES completion:^{}];
}
Cuneo answered 17/11, 2015 at 3:15 Comment(1)
No idea, but 20pts is the size of status bar to me so that is the first place I would look.Encrata
A
13

I also encountered this issue. Searching for other issues about vertical mis-positioning of the left bar button item took me to this question. The gist of it is that this problem occurs, for unknown reasons, if you have a bar button item that has an image, but an empty string as it's title. Set the title to a single space instead of just an empty string:

self.backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];

I don't know if it will fix it for you, but it mostly did for me - the button still does a slight 'jump' animation as though it's being newly created (but only the first time it appears) - but it stays at the same vertical position.

Edit: Passing in nil as the title also removes the extraneous animation. Seems like this is just a peculiarity in how iOS handles whitespace strings as titles.

Auto answered 20/11, 2015 at 0:49 Comment(3)
setting to nil fixed it. Thanks so much! What an incredibly strange bug. =DCuneo
I've caught this bug not only with AlertController. Thanks a lot.Luciano
That is so odd that the fix is to assign value to the title. Thanks for the help!Uncinariasis
F
1
 barbutton.title = nil;

Set title nil and this work for me.

Fahrenheit answered 3/1, 2017 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.