UIAertField中的UITextField(border,backgroundColor)

这是UIAlertController的屏幕截图。 我只是在玩自定义字体和textfield属性,但我无法完成以下操作:

  • 清晰的UITextField背景
  • 没有丑陋的边框(黑匣子)如下图所示

在此处输入图像描述

当我更深入地研究代码和iOS运行时标题时,我能够修改边框和背景颜色,但上述问题仍然存在,因为这些属性属于容器UITextView 。 将背景更改为clearColor没有帮助。

有没有人玩过这个? 不确定我是否会将这些丑陋的文本字段带入我的应用程序。

编辑(5月13日,15日)以下答案由Rory McKinnel测试iOS 8 – 8.3并且工作得很好。 结果如下:

在此处输入图像描述

对此有一些乐趣。 以下似乎有效。 显然,根据需要判断,它没有未来的certificate,是一个远离不工作的补丁。

我通过在调试器中遍历视图层次结构来解决这个问题,我注意到了UIVisualEffectView。 删除它似乎可以为您提供所需的设置,并将包含视图设置为清晰的背景。 在不删除视觉效果的情况下,清晰的背景会出于某种原因显示警报视图背后的内容。

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Its Not Pretty!" message:@"Some times things get ugly!" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.text = @"Text: No border and clear 8^)"; }]; [self presentViewController:alertController animated:TRUE completion:^{ }]; for (UIView* textfield in alertController.textfields) { UIView *container = textField.superview; UIView *effectView = container.superview.subviews[0]; if (effectView && [effectView class] == [UIVisualEffectView class]){ container.backgroundColor = [UIColor clearColor]; [effectView removeFromSuperview]; } } 

这是swift中的重要部分:

 for textfield: UIView in alertController.textfields { var container: UIView = textField.superview var effectView: UIView = container.superview.subviews[0] container.backgroundColor = UIColor.clearColor() effectView.removeFromSuperview() } 

你可以试试这个。 因为您只需要清晰的颜色到alertview的文本字段。 只需在创建alertview后添加代码行。

  UITextField *textField = [alertView textFieldAtIndex:0]; textField.backgroundColor=[UIColor clearColor]; textField.superview.backgroundColor=[UIColor clearColor]; 

编辑alertviewCoontroller你可以添加

 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.backgroundColor=[UIColor clearColor]; textField.superview.backgroundColor=[UIColor clearColor]; }]; 

谢谢,如果有任何混乱,还原。

Swift 2.0版本:

 for textField in alert.textFields! { if let container = textField.superview, let effectView = container.superview?.subviews.first where effectView is UIVisualEffectView { container.backgroundColor = UIColor.clearColor() effectView.removeFromSuperview() } } 

这非常hacky,所以在使用之前检查它(在iOS 8.3上测试):

 UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"This is my placeholder"; textField.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0]; // You can change it to whatever color you want [textField superview].backgroundColor = textField.backgroundColor; [[textField superview] superview].backgroundColor = [UIColor whiteColor]; }]; 

您可以像这样更改边框和背景颜色:

  let subview = alertController!.view.subviews.first! as UIView let alertContentView = subview.subviews.first! as UIView alertContentView.backgroundColor = UIColor.lightGrayColor() alertContentView.layer.cornerRadius = 10; alertContentView.layer.borderWidth = 2; 

Swift 3清晰版

 alertController.textFields?.forEach { $0.superview?.backgroundColor = .clear $0.superview?.superview?.subviews[0].removeFromSuperview() } 
Interesting Posts