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对按钮进行分组并在操作和取消按钮之间留出一些空间?
答案 0 :(得分:10)
线alertVC.view.tintColor = [UIColor copperColor];
导致问题,它使警报控制器的整个视图颜色相同,在第一张图片中取消按钮有白色背景。要解决此问题,请将此行移至函数末尾,即添加所有操作后。
答案 1 :(得分:5)
对我来说,当我将样式设置为UIAlertActionStyleCancel
上的UIAlertAction
时,它就有用了。
在下面的代码中,action
是UIAlertAction
对象,而controller
是带有样式操作表的UIAlertController
。
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];
答案 2 :(得分:0)
将要分开的操作按钮的style:
参数设置为.cancel
,而不是.default
。
像style: .cancel
或更具体地说
// **style: on this is set to .cancel**
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in }
通常是您命名为“取消”的按钮
我不确定按钮文字的颜色如何设置,即操作员说他在回答中所做的方式会影响动作分离的方式。我使用“ titleTextColor”操作来更改文本颜色,并且对操作员询问的间距没有影响。
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
这是4个步骤的代码
func presentActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// 1. style: is set to .default on both of these which will keep them grouped
let blockAction = UIAlertAction(title: "Block", style: .default) { (action) in }
let reportAction = UIAlertAction(title: "Report", style: .default) { (action) in }
// 2. style: is set to .cancel which will separate it from the other two actions
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in }
// 3. set the colors for the action text
blockAction.setValue(UIColor.brown, forKey: "titleTextColor")
reportAction.setValue(UIColor.purple, forKey: "titleTextColor")
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
// 4. add the buttons to the action sheet and make sure the cancel button is last
actionSheet.addAction(blockAction)
actionSheet.addAction(reportAction)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true, completion: nil)
}