首页 > 解决方案 > 以编程方式快速更改 Xib 高度

问题描述

我将 2 个 Xib 文件实现到 View Controller(1 个 View,1 个 CollectionView)。我想在滚动时显示/隐藏视图。我可以隐藏视图 (set height:0) ,但是当我想返回 (set height:50) 时,它不起作用。

提前致谢。

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{

        self.searchFieldView.heightAnchor.constraint(equalToConstant: 0).isActive = true
        self.searchFieldView.clipsToBounds = true
        //self.view.setNeedsLayout()
        self.view.layoutIfNeeded()
    }
    else{

        self.searchFieldView.heightAnchor.constraint(equalToConstant: 50).isActive = true
        //self.view.setNeedsLayout()
        self.view.layoutIfNeeded()
    }

}

标签: iosswiftxcodexib

解决方案


从 xib 中参考 searchFieldView 的高度,并根据条件更改其常量值。

@IBOutlet weak var vwHeightConstraint: NSLayoutConstraint!

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{

        vwHeightConstraint.constant = 0

        self.searchFieldView.clipsToBounds = true
        //self.view.setNeedsLayout()
        self.view.layoutIfNeeded()
    }else{
         vwHeightConstraint.constant = 50

        //self.view.setNeedsLayout()
        self.view.layoutIfNeeded()
    }

}

希望它会起作用。一旦尝试这个


推荐阅读