【问题标题】:How to override the UI change of a UITextField when it gets Focus on tvOS?当 UITextField 在 tvOS 上获得焦点时如何覆盖其 UI 更改?
【发布时间】:2016-03-16 14:29:47
【问题描述】:

我的问题很简单。我在 tvOS 上显示的视图中有一个 UITextField。当 UITextField 获得 Focus 时,textfield 看起来像放大了。效果还不错,即使不受欢迎,但我添加了一个带颜色的边框,并且边框在 textfield 内没有缩放。

这是一个重现的代码示例:

- (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]];
}

【问题讨论】:

  • @JakobMikkelsen 抱歉,调用 setNeedsDisplay 和 setNeedsLayout 不会改变文本字段动画的任何内容。
  • 你有没有同样的解决方案?
  • @DeepKSOreadytohelp 我没有得到任何解决方案。我的后备方案是仅使用默认的基本焦点作为文本字段。另一种部分起作用的解决方法是将文本字段放在相同大小的视图中,并将剪辑到边界设置为 YES :缩放效果仍然存在,但不那么烦人。然后,您可以将边框效果放在超级视图上......但它仍然很蹩​​脚。
  • 谢谢你的时间,我会试试这个,如果我得到更好的解决方案/结果,我会告诉你:)

标签: objective-c uitextfield focus tvos


【解决方案1】:

尝试继承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)
}

【讨论】:

  • 谢谢你的技巧,但它并不适合我并引入了一个新问题。您提供的解决方案只会阻止调用超类的动画,并且可能会产生意想不到的效果,因为我们实际上并不知道超实现中做了什么。而且,当我开始编辑Textfield时,同样奇怪的阴影出现了,并且永远不会消失,因为我们没有调用焦点更新方法的超级实现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多