如何更改UIAlertController的文本框边框颜色

时间:2014-12-05 08:04:40

标签: uitextfield

我尝试在ios中使用UIActionController来显示文本字段,我想知道如何更改文本字段边框颜色。

我的代码是:

- (void)showChangePasswordAlertView
{
    UIAlertController *actionVC = [UIAlertController alertControllerWithTitle:@""
                                                                  message:@"Change your password."
                                                           preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK"
                                                 style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction *action) {
                                               }];
    [actionVC addAction:action];

    action = [UIAlertAction actionWithTitle:@"Cancel"
                                      style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action) {
                                  }];
    [actionVC addAction:action];

    [actionVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
       textField.placeholder = @"old password";
       textField.borderStyle = UITextBorderStyleNone;
       textField.layer.borderColor = [UIColor redColor].CGColor;
       textField.layer.borderWidth = 1.0 ;
      textField.secureTextEntry = YES ;
    }];

    [actionVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"new password";
        textField.borderStyle = UITextBorderStyleNone;
        textField.secureTextEntry = YES ;
    }];

    [actionVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"confirm new password";
        textField.borderStyle = UITextBorderStyleNone;
        textField.secureTextEntry = YES ;
    }];

    [self presentViewController:actionVC animated:YES completion:nil];
}

但结果是:

enter image description here

任何身体都知道该怎么办?感谢。

2 个答案:

答案 0 :(得分:0)

它有点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 superview] superview].backgroundColor = [UIColor redColor];

}];

答案 1 :(得分:0)

也很黑客

显示UIAletController之后:

viewController.present(alert, animated: true, completion: nil)

添加此代码:

YOUR_ALERT_CONTROLLER.textFields.forEach { 
    if let views = $0.superview?.superview?.subviews {
        for case let visualView as UIVisualEffectView in views {
            visualView.contentView.subviews.first?.backgroundColor = CURRENT_COLOR
        }
      }
    }