【问题标题】:Update constraints swift layoutIfNeeded doesn't work更新约束 swift layoutIfNeeded 不起作用
【发布时间】:2018-06-17 10:00:07
【问题描述】:

当请求完成并且我有来自服务器的图像时,我需要更新约束(我的 CollectionView 的高度),因此 View 的高度也会改变。

结果屏幕

我需要什么屏幕

我的代码:

            DispatchQueue.main.async {
                self.cvActivity.alpha = 0
                self.collectionView.reloadData()
                self.collectionView.heightAnchor.constraint(equalToConstant: self.cellWidth * 2).isActive = true

                self.collectionView.setNeedsUpdateConstraints()
                self.collectionView.setNeedsLayout()
                self.view.layoutIfNeeded()
            }

【问题讨论】:

    标签: ios swift layout constraints


    【解决方案1】:

    嗯,@J 的基本想法。 Doe 是正确的,这里有一些代码解释(为简单起见,我使用了UIView,而不是UICollectionView):

    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet var heightConstraint: NSLayoutConstraint! // link the height constraint to your collectionView
        private var height: CGFloat = 100 // you should keep constraint height for different view states somewhere
    
        override func updateViewConstraints() {
            heightConstraint.constant = height // set the correct height
    
            super.updateViewConstraints()
        }
    
        // update height by button press with animation
        @IBAction func increaseHight(_ sender: UIButton) {
            height *= 2
            UIView.animate(withDuration: 0.3) {
                self.view.setNeedsUpdateConstraints()
                self.view.layoutIfNeeded() // if you call `layoutIfNeeded` on your collectionView, animation doesn't work
            }
        }
    
    }
    

    结果:

    【讨论】:

      【解决方案2】:

      为您的 collectionView 定义一个高度,从该约束创建一个出口并增加该约束的常量并在动画块中调用 layoutifneeded

      【讨论】:

      • 对我不起作用:self.collectionViewHeight.constant = self.cellWidth * 2 self.collectionView.layoutIfNeeded()
      • @ВладиславФорафонтов,我相信你应该在collectionView的superview打电话给layoutIfNeeded()
      • @pacification 在什么视图中我必须调用 layoutIfNeeded?现在我只在 collectionView 中调用
      • 我现在的代码:self.collectionViewHeight.constant = self.cellWidth * 2; self.view.layoutIfNeeded(); self.collectionView.reloadData();它也不起作用
      • @ВладиславФорафонтов 看看我的回答stackoverflow.com/a/48150634/2463616
      【解决方案3】:

      您需要从情节提要中创建高度约束的对象

       @IBOutlet weak var YourHeightConstraintName: NSLayoutConstraint!
      
      
        YourConstraintName.constant = valueYouWantToGive
      

      ---------或--------

        collectionViewOutlet.view.frame = CGRect(x: 
          collectionViewOutlet.frame.origin.x , y: 
          collectionViewOutlet.frame.origin.y, width: 
          collectionViewOutlet.frame.width, height: 
          yourheightValue)
      

      【讨论】:

      • 而不是调用setNeedsUpdate等?
      • @ВладиславФорафонтов 不要在异步调度中写这个
      • @ВладиславФорафонтов 无需调用 setNeedUpdate
      • @ВладиславФорафонтов 在 vi​​ewLifeCycle override func viewWillLayoutSubviews() { YourConstraintName.constant = valueYouWantToGive } 的方法中使用此代码
      • 但是我的项目如何知道是时候调用 override func viewWillLayoutSubviews 了?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      相关资源
      最近更新 更多