UIAertField中的UITextField(border,backgroundColor)

p0l*_*ris 12 uitextfield ios uialertcontroller

这是一个截图UIAlertController.我只是在玩自定义字体和textfield属性,但我无法完成以下任务:

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

在此输入图像描述

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

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

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

在此输入图像描述

Ror*_*nel 18

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

我通过在调试器中遍历视图层次结构来解决这个问题,我注意到了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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Matthew在调用presentViewController而不是完成后尝试执行此操作.在目前的电话会议之后,意见应该到位.希望这应该工作. (2认同)

Pau*_*ehn 6

这是 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()
}
Run Code Online (Sandbox Code Playgroud)