【问题标题】:How to hide title/message frame in a UIAlertController?如何在 UIAlertController 中隐藏标题/消息框?
【发布时间】:2015-06-28 22:00:52
【问题描述】:

当使用UIAlertController 时,如果我想呈现带有空标题和空消息的UIActionSheet,则保留标题和/或消息的预期位置的框架。

如何更改此设置,以便我只显示一个 ActionSheet,内容如下:

设置
登出
取消 ?

谢谢!

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    当我使用此代码创建 UIAlertController 时,我没有标题间距。

    [UIAlertController alertControllerWithTitle:nil
                                        message:nil
                                 preferredStyle:UIAlertControllerStyleActionSheet];
    

    你是为标题和消息传递 nil 还是空字符串?

    【讨论】:

    • 这很简单。这回答了我的问题。谢谢
    • 有用的答案。
    • 谢谢!我接手的代码有问题 - 它使用的是空字符串“”而不是 nil。当心!
    【解决方案2】:

    UIAlertController *controller=[UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];//样式检查

    UIAlertAction *first = [UIAlertAction actionWithTitle: @"Login with Facebook" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action)
    {
       //write to perform action
    
    }];
    
    
    [controller addAction: first];
    
    
    
    UIAlertAction *second = [UIAlertAction actionWithTitle: @"Guest User" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action)
    

    { //写入执行操作

    }];
    
    [controller addAction:second];
    
    
    UIAlertAction *third=[UIAlertAction actionWithTitle:@"Registered User" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
    
    
    {
        //write to perform action
    
    }];
    
    [controller addAction:third];
    
    [self presentViewController: controller animated: YES completion: nil];
    

    【讨论】:

      【解决方案3】:

      如果您想根据特定情况更改运行时间,只需编写:

      actionController.title = nil
      actionController.message = nil

      【讨论】:

        【解决方案4】:

        在 swift 2.2 中,您可以使用下面的代码,我还更改了注销操作按钮的颜色

                let actionSheet: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        
            self.presentViewController(actionSheet, animated: true, completion: nil)
        
            let settingsActionButton: UIAlertAction = UIAlertAction(title: "Settings", style: .Cancel) { action -> Void in
                print("Settings Tapped")
            }
        
            reportActionSheet.addAction(settingsActionButton)
        
            let signOutActionButton: UIAlertAction = UIAlertAction(title: "Signout", style: .Default)
            { action -> Void in
                //Clear All Method
                print("Signout Tapped")
        
            }
        
            signOutActionButton.setValue(UIColor.redColor(), forKey: "titleTextColor")
        
            actionSheet.addAction(signOutActionButton)
        
            let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
                print("Cancel Tapped")
            }
        
            reportActionSheet.addAction(cancelActionButton)
        

        【讨论】:

          【解决方案5】:

          更新 Swift 4:

          let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
          

          只需将nil 传递给titlemessage 参数即可。

          【讨论】:

          • 这在 SwiftUI 中不再有效。
          【解决方案6】:

          如果要删除警报控制器顶部区域空间。添加警报控制器高度

          let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
          alert.view.addConstraint(height);
          

          enter image description here

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-26
            • 1970-01-01
            • 2017-02-18
            相关资源
            最近更新 更多