Reputation: 3860
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];
}
Upvotes: 0
Views: 1062
Reputation: 23223
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. :)
Upvotes: 1
Reputation: 5655
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}]];
}
}
Upvotes: 0