繁体   English   中英

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

[英]Swift Change Xib Height Programmatically

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

}

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

}

希望它能正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM