user2924482
user2924482

Reputation: 9120

Swift: Changing the hight of subview programmatically

I'm implementing a subview but I need to change the subview high programmatically but. This is how I'm changing the high:

    func changeHigh() {
        UIView.animate(withDuration: 1.0, animations: {
            self.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height - 50)
            
            self.buttonConstraint.constant = 10
        })
    }

But the problem with this implementation is animating also the position of the view. All I want is to change the high with out changing the position.

Any of you knows why is changing the position of the subview?

I'll really appreciate you help.

Upvotes: 1

Views: 1172

Answers (1)

aheze
aheze

Reputation: 30318

First, to change the height, you can just modify the size.height property of the frame.

self.frame.size.height -= 50 /// this will make it 50 pts less

Also, you should change constraints outside the animation block, not inside. Then, call layoutIfNeeded() to animate it.

Here's how your code should look:

func changeHigh() {
    self.buttonConstraint.constant = 10
    UIView.animate(withDuration: 1.0, animations: {
        self.frame.size.height -= 50
        self.layoutIfNeeded()
    })
}

Upvotes: 2

Related Questions