1
votes

I have a view hierarchy like this:

UIView
  - ADBannerView
  - UIImageView
  - UILabel
  - UIButton
  - UINavigationController
    - UIView

I'm loading the image view, label and button from a nib file and the UINavigationController from another nib. All have autoresizing masks set. I'm creating the ADBannerView programmatically.

Now my problem is that I would like the image, label, button to move down and the navigation controller to shrink when I insert an ADBannerView. However this is not happening, instead the ADBannerView is placed on top of the image and the label.

Can anybody explain to me what am I doing wrong here?

2
I can't say enough good things about Ray Wenderlich's tutorial on integrating iAds into your app. He addresses this issue directly: raywenderlich.com/1371/… - Gavin Miller
Oh, and as a side note... forget about integrating iAds into your app. You're WAY better off integrating AdWhirl which allows you to simultaneously use iAd, AdMob and a bunch of other ad frameworks. adwhirl.com I don't even worry about the others, but the ability to have iAd and AdMob in the same spot is GREAT. iAd only serves ads about 8% of the time, which means you are missing out on 92% of your revenue. Fill the rest with AdMob! - Kenny Wyland

2 Answers

3
votes

In other to get those things to "automatically" shift down when you put in the ADBannerView, you'll need to enclose them in their own view and then change the size and position of that view. Assuming the ADBannerView is 50 pixels tall, you'll want to move that UIView down 50 pixels and reduce its height by 50 pixels.

Assuming that self.enclosingView is the new view that you will use to enclose the image, label and button... and assuming you want to make this animated (you probably do, it usually looks a lot better):

// Start the AdBannerView off of the top of the screen
CGRect adFrame = self.bannerView.frame;
adFrame.origin.x = 0.0;
adFrame.origin.y = 0.0 - adFrame.size.height;
self.bannerView.frame = adFrame;

[UIView beginAnimations:@"Show Ads" context:nil];

// Animate the shrinking of the enclosing view
CGRect enclosingFrame = self.enclosingView.frame;
enclosingFrame.size.height -= self.bannerView.frame.size.height;
enclosingFrame.origin.y += self.bannerView.frame.size.height;
self.enclosingView.frame = enclosingFrame;

// Animate the motion of the bannerView into view
adFrame.origin.y = 0.0;
self.bannerView.frame = adFrame;

[UIView commitAnimations];
1
votes

Autoresizing mask defines size changes on parent's frame changes, not on sibling insertion/removal. You have to adjust frame for corresponding views programmatically. Kenny Wyland already gave you idea how this can be achieved with less pain. Take a look at CGRectDivide method - with it it's easy to split available space between two views.