当前位置: 动力学知识库 > 问答 > 编程问答 >

cocoa touch - Placing iAD banner always at the bottom of the screen(IOS 7)

问题描述:

So I am implementing the singleton iAD system and I got the function that adds the banner on the view, but how can I make sure that the banner is always on the bottom so it works with both 3.5inch and 4inch screen?

Here is my code but the banner won't position correctly, it goes off screen

-(void)attachBannerToViewController:(UIViewController*)viewController

{

if (_iAdBanner == nil) {

NSLog(@"iAdbanner alloc");

_iAdBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];

_iAdBanner.delegate = self;

_iAdBanner.alpha = 0.0;

}

CGRect frame=CGRectZero;

frame.size = _iAdBanner.frame.size;

frame.origin = CGPointMake(0.0, viewController.view.frame.size.height-_iAdBanner.frame.size.height);

[_iAdBanner setFrame:frame];

[viewController.view addSubview:_iAdBanner];

}

网友答案:

At a guess, I'd say you are running this code before you view controller has finished all it's setup. When you run this line of code it will be correct, but by the time the view controller.view gets added to the window it will be wrong.

Consider setting up your auto-resizing flags or constraints to keep the ad banner in the correct position. Which are you using?

_iAdBanner.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;

Constraints.... someone else just posted and answer for setting it up with constraints so I won't duplicate their work. :)

网友答案:

You could use NSLayoutConstraint:

-(void)attachBannerToViewController:(UIViewController*)viewController
{
    if (_iAdBanner == nil) {
        NSLog(@"iAdbanner alloc");
        _iAdBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
        _iAdBanner.delegate = self;
        _iAdBanner.alpha = 0.0;
        _iAdBanner.translatesAutoresizingMaskIntoConstraints = NO;
        [viewController.view addSubview:_iAdBanner];
        [viewController.view addConstraints:
         [NSLayoutConstraint
          constrainsWithVisualFormat:@"|-0-[banner]-0-|"
          options:0
          metrics:nil
          views:@{"banner" : _iAdBanner}]];
        [viewController.view addConstraints:
         [NSLayoutConstraint
          constrainsWithVisualFormat:@"V:[banner]-0-|"
          options:0
          metrics:nil
          views:@{"banner" : _iAdBanner}]];
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: