UITextField in UIAlertController (border, backgroundColor)

前端 未结 8 1834
陌清茗
陌清茗 2020-12-01 16:38

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

相关标签:
8条回答
  • 2020-12-01 17:12

    To address the situation as discussed in @Rory McKinnel and @Matthew where the superview are NULL and address modifying presented view:

    extension UIAlertController {
        override open func viewWillAppear(_ animated: Bool) {
           super.viewWillAppear(animated)
           self.textFields?.forEach {
               $0.superview?.backgroundColor = .color
               $0.superview?.superview?.subviews[0].removeFromSuperview()
           }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 17:14

    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];
        }
    }
    
    0 讨论(0)
  • 2020-12-01 17:14

    You can change the border and background color like this:

        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;
    
    0 讨论(0)
  • 2020-12-01 17:16

    here is the important part in 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()
    }
    
    0 讨论(0)
  • 2020-12-01 17:20

    This is very hacky, so examine it well before using (tested on 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];
    
    }];
    
    0 讨论(0)
  • 2020-12-01 17:27

    You can try this. As you need only clear color to textfield of your alertview. simply add lines of code after your alertview is created.

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

    EDIT for alertviewCoontroller you can add

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

    Thanks, revert if any confusion.

    0 讨论(0)
提交回复
热议问题