以编程方式快速更改Xib高度

时间:2019-01-22 08:15:06

标签: ios swift xcode xib

我实现了2个Xib文件到View Controller(1个View,1个CollectionView)。我想在滚动时显示/隐藏视图。我可以隐藏视图(设置高度:0),但是当我想返回(设置高度: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()
    }

}

1 个答案:

答案 0 :(得分:1)

从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()
    }

}

希望它能正常工作。尝试一下