Isuru
Isuru

Reputation: 31283

UIView not sliding up from the bottom

I'm trying to get a UIView to slide up from the bottom of the screen. I created the UIView as a separate nib file. I'm initially adding the view beyond the view port of the screen and then animating the y value to have it slide up.

self.bannerView = BannerView(frame: CGRect(x: 10, y: self.view.bounds.height, width: self.view.bounds.width - 10, height: 44))
self.collectionView?.addSubview(self.bannerView!)

UIView.animate(withDuration: 0.5) {
    self.bannerView?.frame.origin.y = self.bannerView!.frame.origin.y - 64
}

However it doesn't seem to work. The view is not showing up. But the calculations seem to be correct. I can't figure out what's wrong.

Example project uploaded here.

Upvotes: 1

Views: 719

Answers (3)

elisar4
elisar4

Reputation: 180

One line solution:

self.bannerView?.frame = self.bannerView!.frame.offsetBy(dx: 0, dy: -64)

(paste this inside the animation block)

Upvotes: 1

prabodhprakash
prabodhprakash

Reputation: 3917

You'll have to change the entire frame instead of just the y value - which will not work.

As an illustration - you can refer to https://youtu.be/2kwCfFG5fDA?list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj&t=993 wherein exactly your problem has been demonstrated to be working by changing frame. The example is above video is also in Swift thus suiting to your need in the question.

Upvotes: 2

Claudio Redi
Claudio Redi

Reputation: 68400

I haven't validated your math but try to set the entire frame instead of changing a property of the assigned frame since that won't work.

var frame = self.bannerView!.frame
frame.origin.y = frame.origin.y - 64
self.bannerView?.frame = frame

Upvotes: 0

Related Questions