UIAlertController 或 UITextField 中的 UILabel 类似 UILabel
UILabel in UIAlertController or UITextField alike UILabel
我需要给 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];
}
更新:
这是我得到的:
这是我想做的:
使用这个
textField.placeholder = @"Text: ";
而不是
textField.text = @"Text: ";
我相信您想在 UIAlert for good UI 中添加自定义标签 UI 看。
执行此操作的最佳方法是对自定义 UIView 进行编程,使其感觉和行为类似于 UIAlertView,或者使用 github 中的以下库之一。
无需第三方库即可实现。只需自定义 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)
我需要给 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];
}
更新:
这是我得到的:
这是我想做的:
使用这个
textField.placeholder = @"Text: ";
而不是
textField.text = @"Text: ";
我相信您想在 UIAlert for good UI 中添加自定义标签 UI 看。
执行此操作的最佳方法是对自定义 UIView 进行编程,使其感觉和行为类似于 UIAlertView,或者使用 github 中的以下库之一。
无需第三方库即可实现。只需自定义 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)