ios - 无法在 iOS 11 GM 中更改多个文本字段颜色

标签 ios objective-c xcode uitextfield ios11

在 iOS 10.3 模拟器上工作,但在您点击文本字段之前不能在 iOS 11 上工作。

层次结构:

view hierarchy

示例代码如下。

#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/

相关文章:

ios - 避免 AppDelegate 中出现 "uncaughtExceptionHandler"警告

ios - 使用 Alamofire 将结构上传到服务器

objective-c - NSError 域/自定义域 - 约定和最佳实践

iphone - 试图获得接触点,返回 NaN

objective-c - COCOA: 无法点击取消按钮,UI 挂起且无响应,如何处理?

ios - 在具有多个 View 的应用程序进入前台时执行segue?

ios - 如何更改 UILabel 中链接的颜色?

ios - 在 Swift 3.0 中从异步线程调用 UIAlertController

iOS - CoreBluetooth didDiscoverPeripheral 未被调用

ios - Xcode 版本 5.1.1 (5B1008) - 在 iOS 7.0 而非 7.1 上运行模拟器