【发布时间】:2015-06-28 22:00:52
【问题描述】:
当使用UIAlertController 时,如果我想呈现带有空标题和空消息的UIActionSheet,则保留标题和/或消息的预期位置的框架。
如何更改此设置,以便我只显示一个 ActionSheet,内容如下:
设置
登出
取消
?
谢谢!
【问题讨论】:
标签: ios swift uialertcontroller
当使用UIAlertController 时,如果我想呈现带有空标题和空消息的UIActionSheet,则保留标题和/或消息的预期位置的框架。
如何更改此设置,以便我只显示一个 ActionSheet,内容如下:
设置
登出
取消
?
谢谢!
【问题讨论】:
标签: ios swift uialertcontroller
当我使用此代码创建 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 = nilactionController.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 传递给title 和message 参数即可。
【讨论】:
如果要删除警报控制器顶部区域空间。添加警报控制器高度
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);
【讨论】: