在 iOS 10.3 模拟器上工作,但在您点击文本字段之前不能在 iOS 11 上工作。
层次结构:
示例代码如下。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) UITextField *textField1;
@property (nonatomic) UITextField *textField2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 50, 30)];
textField.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField];
self.textField1 = textField;
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 50, 30)];
textField2.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField2];
self.textField2 = textField2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 50, 50)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.textField1];
[self.view addSubview:self.textField2];
[self.view addSubview:button];
}
- (void)changeColor
{
self.textField1.textColor = [UIColor greenColor];
self.textField2.textColor = [UIColor greenColor];
}
最佳答案
试试这个。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) UITextField *textField1;
@property (nonatomic) UITextField *textField2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 50, 50, 30)];
textField.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField];
self.textField1 = textField;
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 50, 30)];
textField2.borderStyle = UITextBorderStyleLine;
[self.view addSubview:textField2];
self.textField2 = textField2;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 50, 50)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.textField1];
[self.view addSubview:self.textField2];
[self.view addSubview:button];
}
- (void)changeColor
{
self.textField1.textColor = [UIColor greenColor];
self.textField2.textColor = [UIColor greenColor];
self.textField1.text = self.textField1.text;
}
只需重置 textfiled1 文本即可。
关于ios - 无法在 iOS 11 GM 中更改多个文本字段颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199844/