菜鸟教程小白 发表于 2022-12-11 19:13:38

iOS swift : Constraints did not update on screen


                                            <p><p>晚上,</p>

<p>我遇到了 ScrollView 约束的问题。</p>

<p>这是我的代码:</p>

<pre><code>// when the keyboard is shown, move up some elements
    @objc func keyboardWillShow(notification: NSNotification) {
      if let keyboardSize = (notification.userInfo? as? NSValue)?.cgRectValue {

            // Bring up the scrol lview
            print(scrollViewBottomConstraint.constant)
            self.scrollViewBottomConstraint.constant = keyboardSize.height
            print(scrollViewBottomConstraint.constant)
            self.view.updateConstraints()
            self.view.updateConstraintsIfNeeded()
            self.view.needsUpdateConstraints()
            self.view.layoutIfNeeded()

            // Bring up the next arrow with animation
            self.nextButtonBottomConstraint.constant = keyboardSize.height + 8
            UIView.animate(
                withDuration: 1.0,
                delay: 0.0,
                usingSpringWithDamping: 0.5,
                initialSpringVelocity: 6.0,
                options: [.allowUserInteraction,
                        .curveEaseInOut],
                animations: {
                  self.view.layoutIfNeeded()
            }, completion: nil)
      }
    }
</code></pre>

<p>控制台打印 0 和 253,因此约束常量被更新。但在屏幕上我看不到变化,我有这个日志:</p>

<blockquote>
<p> Snapshotting a view (0x7fa041471bc0,
UIInputSetHostView) that has not been rendered at least once requires
afterScreenUpdates:YES.</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>很奇怪,更改约束后只需 view.layoutIfNeeded() 就可以了。</p>

<p>尝试删除:</p>

<pre><code>self.view.updateConstraints()
self.view.updateConstraintsIfNeeded()
self.view.needsUpdateConstraints()
</code></pre>

<p>只是:</p>

<pre><code> view.layoutIfNeeded()
</code></pre>

<p>试试这个:</p>

<pre><code>@objc func keyboardWillShow(notification: NSNotification) {
    guard let keyboardSize = (notification.userInfo? as? NSValue)?.cgRectValue else{
       return
    }

    scrollViewBottomConstraint.constant = keyboardSize.height
    nextButtonBottomConstraint.constant = keyboardSize.height + 8
    view.layoutIfNeeded()
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOSswift: Constraints did not update on screen,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47157215/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47157215/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS swift : Constraints did not update on screen