如何在 UIAlertController 中隐藏 title/message 框架?

How to hide title/message frame in a UIAlertController?

当使用 UIAlertController 时,如果我想呈现一个带有空标题和空消息的 UIActionSheet,标题 and/or 消息的预期放置框架将保留.

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

设置
登出
取消 ?

谢谢!

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

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

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

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];

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

actionController.title = nil
actionController.message = nil

在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)

更新Swift 4:

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

只需要将nil传递给titlemessage参数。

如果你想删除警报控制器顶部区域space。添加警报控制器高度

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