문제 설명
UIAlertController는 leftBarButtonItem을 아래로 이동합니다. (UIAlertController moves leftBarButtonItem down)
UIAlertControllerStyleAlert
의 기본 스타일로 UIAlertController
를 만들었습니다. leftBarButtonItem
을 탭하면 경고가 표시됩니다. backButton
이라는 UIBarButtonItem
속성을 만들고 leftBarButtonItem = self.backButton
을 설정했습니다. 이것은 설계된 대로 작동합니다. 저는 스토리보드를 사용하지 않습니다.
문제는 경고가 표시될 때 leftBarButtonItem
이 아래로 이동한다는 것입니다(내 추측: 약 20pts). 왜 이런 일이 발생합니까?
버튼이 아래로 이동했을 때 사용자가 버튼을 볼 수 없도록 버튼을 표시/숨기는 방법을 알고 있습니다. 그러나 그것은 짜증난다. 처음에 왜 이런 일이 발생합니까?
나는 하지 않았습니다. 온라인에서 유사한 문제를 모든 발견했습니다.
@property (strong, nonatomic) IBOutlet UIBarButtonItem *backButton;
viewDidLoad:
6self.backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[self.backButton setImage:[UIImage imageNamed:@"back‑arrow‑grey"]];
self.navigationItem.leftBarButtonItem = self.backButton;
backButtonPressed:
6{
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:^{}];
}
참조 솔루션
방법 1:
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.
방법 2:
barbutton.title = nil;
Set title nil and this work for me.