简体   繁体   中英

Autolayout constraint with swift only applied after tap

I am showing an iAD banner like this:

override func viewDidLoad() {
  ...
  initializeBannerAd()
}

func initializeBannerAd() {
  let banner = ADBannerView(adType: ADAdType.Banner)
  banner.frame = CGRectMake(0,-100, self.view.frame.width, 100)
  banner.delegate = self
  banner.hidden = true
  self.view.addSubview(banner)
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
  banner.hidden = false
  banner.frame = CGRectMake(0, self.view.frame.size.height-100, self.view.frame.width, 100)

  banner.setTranslatesAutoresizingMaskIntoConstraints(false)
  let viewsDictionary = ["banner":banner]
  self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[banner]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary))
  self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[banner]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary))
  self.view.layoutIfNeeded()
}

my problem:

  • The first banner loads perfectly and sits snug at the bottom of the screen
  • Whenever the banner updates, it jumps up und stays there until i tap anywhere on the view, then it moves back to the bottom

how can i fix this? why is the constraint applied only after i tap?

The simplest way for you to add an ADBanner at the bottom of a single UIViewController is setting it's property var canDisplayBannerAds: Bool to true. This will trigger an automatic banner presentation without the need to instantiate and add the banner yourself.

With regard to your code, the first time you see the banner, the auto layout constraints you add in bannerViewDidLoadAd are not yet added. Then they will be added again and again every time a new banner is loaded. You'd better add the constraints in viewDidLoad once and for all (in case this is the way you intend to present the banner). Since ADBannerView has intrinsic content size defined, you do not need to force the banner size in code. Just add the auto layout rules.

In case you need an example, here you can find one that I did some time ago for similar questions. In the example I use an ADBannerView singleton and present it at the bottom or top of a viewController's view.

Hope it can help.

here you may be trying to create Adbanner size more than 50 thats why you are getting Adbanner little bit above

iAd supports different banner sizes for portrait and landscape apps. The exact size of advertisements depends on the device the banner is being shown on. On an iPhone, a portrait advertisement is 320 x 50 points and 480 x 32 points for a landscape advertisement.

Ref. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/iAd_Guide/BannerAdvertisements/BannerAdvertisements.html

Have you tried something like this?

 func initializeBannerAd() { let banner = ADBannerView(adType: ADAdType.Banner) banner.setTranslatesAutoresizingMaskIntoConstraints(false) banner.delegate = self banner.hidden = true self.view.addSubview(banner) var constraints = [AnyObject]() constraints.append(NSLayoutConstraint(item: banner, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)) constraints.append(NSLayoutConstraint(item: banner, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)) constraints.append(NSLayoutConstraint(item: banner, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1.0, constant: 0.0)) constraints.append(NSLayoutConstraint(item: banner, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100.0)) NSLayoutConstraint.activateConstraints(constraints) } func bannerViewDidLoadAd(banner: ADBannerView!) { banner.hidden = false self.view.layoutIfNeeded() } 

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