Search code examples
iosanimationiadadbannerview

views constrained to banner view jump suddenly instead of slowly animating


I am trying to animate banner view from the bottom to the screen using constraints. the button is attached to the bannerview and it should always be at the same distance from banner. This is what I get: https://vid.me/F008 As you can see the button jumps suddenly, but I wanted it to move slowly with UIView.animateWithDuration method. This is my code

@IBOutlet weak var bannerToBottomGuideConstraint: NSLayoutConstraint!
@IBOutlet weak var buttonToBannerConstraint: NSLayoutConstraint!


@IBOutlet weak var bannerView: ADBannerView!


override func viewDidLoad() {
    super.viewDidLoad()

    self.canDisplayBannerAds = true
    bannerToBottomGuideConstraint.constant -= CGRectGetHeight(bannerView.bounds)
    bannerView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    let time = 0.6

    if banner.bannerLoaded == false{
        self.view.layoutIfNeeded()
        bannerToBottomGuideConstraint.constant += CGRectGetHeight(bannerView.bounds)
        UIView.animateWithDuration(time, animations: {
            self.view.layoutIfNeeded()
            }, completion: {_ in
        })
    }
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {

    if bannerView.bannerLoaded{
        bannerToBottomGuideConstraint.constant -= CGRectGetHeight(bannerView.bounds)
        UIView.animateWithDuration(0, animations: {
            self.view.layoutIfNeeded()
            }, completion: {_ in
        })
    }
}

Solution

  • Instead of self.view.layoutIfNeeded() in the animation block, call banner.superview!.layoutIfNeeded().