2014-12-05 105 views
7

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

我的代码是:

- (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

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

+1

您是否解决了问题? – 2015-02-12 11:51:37

回答

0

这是一个有点哈克,但它肯定工程(在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]; 

}]; 
+0

这个答案在9.x中不适用于我。 – Hima 2016-04-18 08:24:56

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