jun*_*dev 9 objective-c uinavigationcontroller uinavigationitem uialertcontroller
我创造了一个UIAlertController首选的风格UIAlertControllerStyleAlert.leftBarButtonItem点击时会显示警报.我创建了一个UIBarButtonItem名为的属性backButton并设置了leftBarButtonItem = self.backButton.这是按设计工作的.我没有使用故事板.
问题是leftBarButtonItem当警报显示时,向下移动(我的猜测:大约20分钟).为什么会这样?
我知道如何显示/隐藏按钮,以便用户在向下移动时看不到该按钮.然而,这很糟糕.为什么它首先发生?
我没有在网上找到任何类似的问题.
@property (strong, nonatomic) IBOutlet UIBarButtonItem *backButton;
Run Code Online (Sandbox Code Playgroud)
在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;
Run Code Online (Sandbox Code Playgroud)
在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:^{}];
}
Run Code Online (Sandbox Code Playgroud)
Xon*_*ono 13
我也遇到过这个问题.搜索有关左栏按钮项目垂直错误定位的其他问题让我想到了这个问题.它的要点是出现这个问题,原因不明,如果你有一个带有图像的条形按钮项目,但是一个空字符串作为它的标题.将标题设置为单个空格而不是空字符串:
self.backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
Run Code Online (Sandbox Code Playgroud)
我不知道它是否会为你修复它,但它主要是为我做的 - 按钮仍然会做一个轻微的"跳跃"动画,好像它是新创建的(但只是第一次出现) - 但它仍然保留在相同的垂直位置.
编辑:传递为nil,因为标题也删除了无关的动画.似乎这只是iOS如何将空白字符串作为标题处理的特性.
| 归档时间: |
|
| 查看次数: |
967 次 |
| 最近记录: |