IOS7 auto-layout, view size and iAds

I am using Auto Layout in my iOS 7 project with the following view hierarchy

Main page
-Container view
--- Button
--- Button
--- ImageView
-Banner View (iAd Banner View)

Main View and Container View - full screen width and height. I have horizontal and vertical spatial limitations in the container view, attached to the main view (screen height and width). Also, the Container View sub-items are limited to the view button with a 20px space.

My problem arises when the Banner View is finally filled and placed at the bottom of the screen, after which I have a container view that subtracts the banner viewing height from its frame height to allow space for viewing the banner. (code used below). The ideal result is to look at the container to subtract the height and update the restrictions on subviews based on this new height, but ultimately this means that iAD Banner View simply overlays the view, as shown in the figure.

Code for BannerViewDidLoadAd:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner { CGRect contentFrame = self.containerView.bounds; CGRect bannerFrame = self.bannerView.bounds; if (self.bannerView.bannerLoaded) { contentFrame.size.height = self.containerView.frame.size.height - self.bannerView.frame.size.height; bannerFrame.origin.y = contentFrame.size.height;; } else { bannerFrame.origin.y = contentFrame.size.height; } [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ [self.containerView setFrame:contentFrame]; [self.containerView layoutIfNeeded]; self.bannerView.frame = bannerFrame; self.bannerView.hidden = NO; }]; [self.containerView updateConstraints]; } 

Image of iAd overlaying Container View and its SubViews

iad overlaying

+8
ios objective-c autolayout ios7 iad
source share
2 answers

After you create the banner view in the code (and add it as a subpoint of the main view), you must add a space length limit between the bottom of the container view and the top of the banner view (banner view will require restrictions on both sides of the main view and height limit) . The container view should have a length limit of 0 for all four edges of the main view. You have to do an IBOutlet for this lower limit and animate this constant value of the restriction by an amount equal to the height of the banner view (therefore, it will decrease and the banner view will move up with it because of its vertical length limit by length 0). So, if the exit to the lower limit was called bottomCon, and the banner presentation height was 100 points, you would animate like this:

 [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ self.bottomCon.constant = 100; [self.mainView layoutIfNeeded]; }]; 

There is no need to hide and display the view, since in any case you will first place it at the bottom of the screen. Also, make sure you call [bannerView setTranslatesAutoresizingMaskIntoConstraints:NO] immediately after creating the banner view, or get automatic layouts when the application starts.

+8
source share

The answer from rdelmar was enough for me to work, but I will add a few things. With automatic layout, there is no need to set the banner size using setAutoresizingMask: UIViewAutoresizingFlexibleWidth (and currentContentSizeIdentifier is deprecated in iOS 6). Just create a banner object and then clip it in the right place using the procedure described by rdelmar, and the automatic layout takes care of the horizontal size.

Here are the limitations that I used:

 // pin sides to superview [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_bannerView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_bannerView)]]; // set height to a constant [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_bannerView(==66)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_bannerView)]]; // pin contentView to bannerView with 0 length constraint [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_contentView]-0-[_bannerView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_contentView,_bannerView)]]; 

I was concerned about setting a height limit because the height of the banner will vary depending on the platform and / or orientation. But it doesn't seem to matter what value I set to limit the height - the banner always displays with the correct height, so I don’t even want to set it. I guess this is because there is an internal rating at the height of banner ads.

+3
source share

All Articles