如何在关注tvOS时覆盖UITextField的UI更改?

时间:2016-03-16 14:29:47

标签: objective-c uitextfield focus tvos

我的问题非常简单。我在tvOS上显示的视图中有一个UITextField。当UITextField获得焦点时,文本字段看起来像放大。效果不错,即使不受欢迎,但我添加了一个彩色边框,并且边框未在文本字段中缩放。

以下是重现的代码示例:

- (void)viewDidLoad {
    [super viewDidLoad];

    UITextField* textFieldExample1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 700, 100)];
    [[self view] addSubview:textFieldExample1];
    [[textFieldExample1 layer] setBorderWidth:2];
    [[textFieldExample1 layer] setBorderColor:[[UIColor cyanColor] CGColor]];

    UITextField* textFieldExample2 = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, 700, 100)];
    [[self view] addSubview:textFieldExample2];
    [[textFieldExample2 layer] setBorderWidth:2];
    [[textFieldExample2 layer] setBorderColor:[[UIColor cyanColor] CGColor]];
}

Textfield with focus

Textfield without focus

1 个答案:

答案 0 :(得分:0)

尝试继承UITextField,然后在那里实现didUpdateFocusInContext。

这样的事情:

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

    coordinator.addCoordinatedAnimations({ () -> Void in

        // remove or give custom look to your shadow here

        }, completion: nil)
}