【问题标题】:In new xcode layoutSubviews is not called在新的 xcode layoutSubviews 中没有被调用
【发布时间】:2018-03-31 06:34:29
【问题描述】:

我的代码可以为视图设置动画(更改约束)

UIView .animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {

        self.topConstraint!.constant = self.yPositioOfOpenPanel
        self.superview?.layoutIfNeeded()

    }

奇怪的行为是在 iPhone X 上layoutSubviews 被调用,但在其他模拟器和真实设备上却没有。 (我认为应该是)

同样的行为是当我手动更改约束时

if let constraint = topConstraint {
            constraint.constant = self.superview!.frame.height - recogniser.location(in: self.superview).y + yPositionOfBeginTouchInPanel
        }

编辑:

按照建议,我正在添加一些新信息。

动画效果很好,一切都在动画中。

问题是我想在动画到位时触发一些新事件。 (我会使用 topConstraint.constant 在动画到位时计算某些东西,或者当我手动更改 topConstraint 时)这就是为什么我想在方法 layoutSubviews 上设置一些触发器。当动画更改子视图时应该触发此方法。

我认为这在 iOS 11 之前有效(但我不能 100% 确定,因为这个功能在我的程序中是新的,但我记得我在其他程序中使用过它并且它有效)。它仍然适用于 iPhone X(模拟器),但不适用于其他模拟器或真实设备(显然真正的 iPhone X 尚未推出)。

【问题讨论】:

    标签: ios xcode9 iphone-x layoutsubviews


    【解决方案1】:

    我认为你应该稍微改变一下动画代码:

    // update constraints before you call animate
    self.topConstraint!.constant = self.yPositioOfOpenPanel
    // tell superview that the layout will need to be updated
    self.superview?.setNeedsLayout()
    
    UIView.animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
        // here you tell it to update the layout according to new constraints
        self.superview?.layoutIfNeeded()
    }
    

    【讨论】:

    • 但是为什么这段代码在 iOS 11 之前可以运行,并且仍然可以在 iPhone X 模拟器上运行?
    • 以前有用吗?也许您应该编辑问题以包含该信息。所以我的建议没有帮助,是吗?
    • 我编辑我的问题。不,你的答案不起作用。认为我的动画效果很好,只有方法 layoutSubviews 没有被调用。
    【解决方案2】:

    原来我需要做的是:

    UIView .animate(withDuration: TimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
    
            self.topConstraint!.constant = self.originalConstraintConstant
            self.superview?.layoutIfNeeded()
            self.layoutIfNeeded()
    
        }
    

    这触发了此视图内的layoutIfNeeded

    但我仍然不知道为什么这适用于所有 iPhone,而其他仅适用于 iPhone X。这应该是 iOS 或 Xcode 内部的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多