0

我有一个自定义的 uiView,它有两个按钮。我只是希望能够在按钮变为或离开焦点时更改按钮的高度和宽度

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {


     if context.nextFocusedView == noButton{
         print("About to be a lot of no in here")
         coordinator.addCoordinatedAnimations({
self.yesButton.widthAnchor.constraint(equalToConstant: 360).isActive = true
self.yesButton.heightAnchor.constraint(equalToConstant: 140).isActive = true
self.yesButton.layoutIfNeeded()
         }) {
self.noButton.widthAnchor.constraint(equalToConstant: 400).isActive = true
self.noButton.heightAnchor.constraint(equalToConstant: 860).isActive = true
self.noButton.layoutIfNeeded()
         }



     }

     if context.nextFocusedView == yesButton{
         coordinator.addCoordinatedAnimations({
             self.noButton.widthAnchor.constraint(equalToConstant: 360).isActive = true
             self.noButton.heightAnchor.constraint(equalToConstant: 140).isActive = true
             self.noButton.layoutIfNeeded()
         }) {
             self.yesButton.widthAnchor.constraint(equalToConstant: 400).isActive = true
             self.yesButton.heightAnchor.constraint(equalToConstant: 160).isActive = true
             self.yesButton.layoutIfNeeded()
         }
     }

 }

我只是想不通为什么我不能用它来改变按钮的高度/宽度。我知道我做错了什么(如果愚蠢,请原谅。

4

1 回答 1

0

您需要layoutIfNeeded()在更新它们的约束后调用您的按钮。

于 2019-11-22T23:34:27.053 回答