在聚焦时,在tvOS中使用UITextField边框

时间:2016-07-22 13:35:34

标签: uitextfield focus tvos

我已经为UITextField图层设置了自定义边框。当UITextField聚焦时,视图会变大。但该层不是大型的。因此,在视图中间绘制边框我做错了什么?

1 个答案:

答案 0 :(得分:4)

这似乎是layer properties not getting transformed when being focused的已知问题。

与此同时,您可以通过继承UITextField并实现您想要的焦点行为来提供自定义焦点行为。例如:

class TextField: UITextField  {
var borderLayer = CALayer()
override func awakeFromNib()
{
    super.awakeFromNib()
    self.borderLayer.borderColor = UIColor.blackColor().CGColor
    self.borderLayer.borderWidth = 2.0
    self.borderLayer.cornerRadius = 5.0
    self.borderLayer.frame = self.bounds
    self.layer.addSublayer(self.borderLayer)
}

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
{
    super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
    if context.nextFocusedView === self
    {
        coordinator.addCoordinatedAnimations({
            self.borderLayer.bounds = self.expandedBounds()
        }){}
    }
    else if context.previouslyFocusedView === self
    {
        coordinator.addCoordinatedAnimations({
            self.borderLayer.frame = self.bounds
        }){}
    }
}

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?)
{
    super.pressesBegan(presses, withEvent: event)
    self.borderLayer.bounds = self.bounds
}


override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?)
{
    super.pressesEnded(presses, withEvent: event)
    self.borderLayer.bounds = self.expandedBounds()
}

func expandedBounds() -> CGRect
{
    let insetX:CGFloat = self.bounds.height * 0.3333
    let insetY:CGFloat = self.bounds.height * 0.075
    return CGRectInset(self.bounds, -insetX, -insetY)
}    } 

这里我们添加了一个带边框的图层,并在聚焦或按下时调整它的边界。在我看来,还有更多的工作,而不是它的价值。