在UIAlertController中的操作下方显示空格

问题描述 投票:6回答:3

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对按钮进行分组并在操作和取消按钮之间留出一些空间?

ios ios8 uialertcontroller
3个回答
10
投票

alertVC.view.tintColor = [UIColor copperColor];引起问题,它使警报控制器的整个视图颜色相同,在你的第一张图片中取消按钮有白色背景。要解决此问题,请将此行移至函数末尾,即添加所有操作后。


5
投票

对我而言,当我在UIAlertActionStyleCancel上将样式设置为UIAlertAction时,它就起作用了。

在下面的代码中,actionUIAlertAction对象,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];

0
投票

设置要分隔到style:而不是.cancel的操作按钮的.default参数。

像这样style: .cancel

或者更具体地说

// **style: on this is set to .cancel**
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in }

它通常是您命名为“取消”的按钮

enter image description here

我不确定按钮文本设置的颜色与操作说他在答案中所做的一样会影响操作分离的方式。我使用动作“titleTextColor”来改变文本颜色,它对op询问的间距没有影响。

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.