UIAlertController 向下移动 leftBarButtonItem [英] UIAlertController moves leftBarButtonItem down
问题描述
我创建了一个 UIAlertController
,其首选样式为 UIAlertControllerStyleAlert
.当点击 leftBarButtonItem
时会显示警报.我创建了一个名为 backButton
的 UIBarButtonItem
属性,并设置了 leftBarButtonItem = self.backButton
.这是按设计工作的.我没有使用故事板.
问题是当警报显示时 leftBarButtonItem
向下移动(我的猜测:大约 20pts).为什么会发生这种情况?
我知道如何显示/隐藏按钮,以便用户在按钮向下移动时看不到该按钮.然而,这很糟糕.为什么它首先发生?
我在网上没有发现任何类似的问题.
@property (strong, nonatomic) IBOutlet UIBarButtonItem *backButton;
在 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;
在 backButtonPressed 中:
<预><代码>{self.navigationItem.leftBarButtonItem = nil;//隐藏backButton,因为它向下移动UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"My title" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *actionLeave = [UIAlertAction actionWithTitle:@"Leave" style:UIAlertActionStyleDefault handler:...//工作正常UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Go back" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {self.navigationItem.leftBarButtonItem = self.backButton;//现在再次显示backButton,警报被解除//这里发生的其他事情按设计工作}];[alertController addAction:actionLeave];[alertController addAction:actionCancel];[self presentViewController:alertController 动画:YES 完成:^{}];}我也遇到了这个问题.搜索有关左栏按钮项垂直错误定位的其他问题让我到 这个问题.其要点是,由于未知原因,如果您有一个带有图像的条形按钮项,但标题为空字符串,则会出现此问题.将标题设置为单个空格而不是空字符串:
self.backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
我不知道它是否会为你修复它,但它对我来说主要是 - 该按钮仍然会做一个轻微的跳跃"动画,就好像它是新创建的一样(但只是第一次出现) - 但是它保持在相同的垂直位置.
编辑:传入 nil 作为标题也会删除无关的动画.似乎这只是 iOS 如何将空白字符串作为标题处理的一个特点.
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:^{}];
}
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.
这篇关于UIAlertController 向下移动 leftBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!