【问题标题】:Show a space below actions in UIAlertController在 UIAlertController 中的操作下方显示一个空格
【发布时间】:2015-02-12 21:05:32
【问题描述】:

iOS 8 之前的版本允许我创建一个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


    【解决方案1】:

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

    【讨论】:

    • 这解决了问题!你能帮我理解为什么设置色调会导致整个视图最终组合在一起吗?
    • 为此你必须继承一个 UIAlertController,旧的风格是这样的:>, <_uialertcontrolleractionview: action="<UIAlertAction:" title="Cancel" descriptive="(null)" image="0x0">>,中间有12pt的间隙,是清晰的颜色,提前设置tintcolor好像让间隙消失了,新样式有一个包含 3 个单元格的集合视图,每个单元格高度为 44。
    • 旧样式有一个带有 2 个单元格的集合视图,还有一个间隙,我不确定它是如何发生的。您可能会进行更多调查以得出一个解释。
    • 这对我来说根本没有意义,但这无论如何都有效。 +1
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      快速回答

      将要分离的操作按钮的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)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        相关资源
        最近更新 更多