Ali Ihsan URAL
Ali Ihsan URAL

Reputation: 1974

Swift Change Xib Height Programmatically

I implemented 2 Xib file to View Controller (1 View , 1 CollectionView). I want to show/hide the view when scrolling. I can hide view (set height:0) , but when I want to get back (set height:50) , it's not working.

Thanks in advance.

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

}

Upvotes: 0

Views: 1122

Answers (1)

Sairam
Sairam

Reputation: 17

Take reference of height of the searchFieldView from xib and change its constant value based on condition.

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

}

Hope it will work.once try this one

Upvotes: 1

Related Questions