作者:whglwz | 来源:互联网 | 2023-05-22 11:16
8之前的iOS版本允许我创建一个UIActionsheet
显示一组按钮,一些空格,然后是取消按钮的版本.像这样的东西:
但是在iOS 8中,当我尝试创建相同的外观时,我最终会得到如下所示的内容:
iOS 8中的代码如下所示:
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC.view setTintColor:[UIColor copperColor]];
UIAlertAction* notifyViaPush = [UIAlertAction
actionWithTitle:@"Send Alert to my phone"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* notifyViaEmail = [UIAlertAction
actionWithTitle:@"Notify me by email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
[alertVC addAction:notifyViaPush];
[alertVC addAction:notifyViaEmail];
[alertVC addAction:cancel];
[self presentViewController:alertVC animated:YES completion:nil];
如何使用UIAlertController对按钮进行分组并在操作和取消按钮之间留出一些空间?
1> gabbler..:
该行alertVC.view.tintColor = [UIColor copperColor];
导致问题,它使警报控制器的整个视图颜色相同,在您的第一张图片中取消按钮具有白色背景.要解决此问题,请将此行移至函数末尾,即添加所有操作后.
2> Itzdsp..:
对我而言,当我将风格设置为时,它就起作用UIAlertActionStyleCancel
了UIAlertAction
.
在下面的代码中,action
是一个UIAlertAction
对象,并且controller
是一个UIAlertController
带样式的Action表.
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
self.tabBarView.selectedIndex = 1;
[self.tabBarView setSelectedIndex:self.tabBarView.selectedIndex];
}];
[controller addAction:action];
[controller addAction:action2];