我需要添加一些标签到UIAlertController,经过研究发现,没有正常的方法去做它。但是,由于可以添加UITextField,我决定将UITextField的外观更改为类似的UILabel (没有边框和背景色)。但是,将其背景色更改为透明,将边框样式更改为none没有帮助。我如何做到这一点?
以下是代码:
- (void)test
{
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Test Title"
message:@"Test Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do Some action here
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.text = @"Text: ";
textField.backgroundColor = [UIColor blueColor];
textField.borderStyle = UITextBorderStyleNone;
textField.backgroundColor = [UIColor clearColor];
[textField setUserInteractionEnabled:NO];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
}];
[self presentViewController:alert animated:YES completion:nil];
}更新:
我得到的是:

下面是我想做的事:

发布于 2016-06-08 07:51:38
我相信您希望在UIAlert中添加一个自定义标签,以获得良好的UI外观。
要做到这一点,最好的方法是编写自定义UIView,使其感觉和行为类似于UIAlertView,或者使用github中的以下库之一。
发布于 2016-06-08 07:34:08
使用这个
textField.placeholder = @"Text: ";而不是
textField.text = @"Text: ";发布于 2020-08-26 07:22:28
不需要第三方库来实现这一点。只需自定义UIAlertController
let alertController = UIAlertController(title: "Add Table", message: "", preferredStyle: UIAlertController.Style.alert)
alertController.addTextField { (textField) -> Void in
textField.text = "No of Columns :"
textField.keyboardType = .numberPad
textField.isUserInteractionEnabled = false
}
alertController.addTextField { (textField) -> Void in
textField.placeholder = "No of Columns"
textField.keyboardType = .numberPad
textField.text = "2"
textField.isUserInteractionEnabled = false
}
alertController.addTextField { (textField) -> Void in
textField.text = "No of Rows :"
textField.keyboardType = .numberPad
textField.isUserInteractionEnabled = false
}
alertController.addTextField { (textField) -> Void in
textField.placeholder = "No of Rows"
textField.keyboardType = .numberPad
textField.text = "2"
}
let saveAction = UIAlertAction(title: "Create", style: UIAlertAction.Style.default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.destructive, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addAction(cancelAction)
alertController.addAction(saveAction)
if let textFields = alertController.textFields {
if textFields.count > 0{
textFields[0].superview!.superview!.subviews[0].removeFromSuperview()
textFields[0].superview!.backgroundColor = UIColor.clear
}
if textFields.count > 2{
textFields[2].superview!.superview!.subviews[0].removeFromSuperview()
textFields[2].superview!.backgroundColor = UIColor.clear
}
}
self.present(alertController, animated: true, completion: nil)

https://stackoverflow.com/questions/37695744
复制相似问题