Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

ios - UITextField in UIAlertController (border, backgroundColor)

Here is a screenshot of a UIAlertController. I was just playing around custom fonts and textfield properties but I was unable to accomplish the following:

  • clear background of the UITextField
  • no ugly border (black box) as shown below

enter image description here

As I dived more into the code and iOS runtime headers, I was able to modify border and background color but the above issue still remains as those properties belong to a container UITextView. Changing background to clearColor doesn't help.

Has anybody ever played around with this? Not sure if I would ever take my app into production with such ugly text fields.

EDIT (May 13, 15) The answer below by Rory McKinnel is tested for iOS 8 - 8.3 and works just fine. The result is below:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Had some fun with this. The following seems to work. Obviously judging by what was required, it has no future proofing and is a patch away from not working.

I figured this out by walking the view hierarchy in the debugger, from which I noticed a UIVisualEffectView. Removing that seems to give you what you want along with setting the containing view to a clear background. Without removing the visual effect, a clear background shows what is behind the alert view itself for some reason.

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];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...