简体   繁体   中英

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 .

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.

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

One line solution:

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

(paste this inside the animation block)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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