聚焦时 tvOS 中的 UITextField 边框

Man*_*tas 3 focus uitextfield tvos

我已经为 UITextField 层设置了一个自定义边框。当 UITextField 获得焦点时,视图会变大。但该层没有放大。因此在视图中间绘制了边框我做错了什么?

bey*_*ulf 5

这似乎是一个已知问题,在聚焦时图层属性不会被转换

同时,您可以通过子类化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)
}    } 
Run Code Online (Sandbox Code Playgroud)

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