I'm using AdMob to display ads in my app. I implemented my ads as "Smart Banner" ... Everything seems to be working fine but my problem is the black fill spaces that the banner has when it has to adapt to the new dimensions ...
Is there a way to cover these black spaces with another color?
This is my implementation
- (void)viewDidLoad {
[super viewDidLoad];
_banner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
_banner.translatesAutoresizingMaskIntoConstraints = NO;
_banner.adUnitID = #"ca-app-pub-3940256099942544/2934735716";
_banner.rootViewController = self;
[_banner loadRequest:[GADRequest request]];
[self.view addSubview:_banner];
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:#[
[guide.leftAnchor constraintEqualToAnchor:_banner.leftAnchor],
[guide.rightAnchor constraintEqualToAnchor:_banner.rightAnchor],
[guide.bottomAnchor constraintEqualToAnchor:_banner.bottomAnchor]
]];
}
Related
I am new to iOS application development and I am currently working on an existing iOS application written using Objective-C. I came across a requirement where I need to implement Safe Area programmatically. So I have implemented the following piece of code in "viewDidLoad" method.
if(#available(iOS 11, *)){
UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
[self.view.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
[self.view.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
[self.view.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[self.view.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
}else{
UILayoutGuide * margins = self.view.layoutMarginsGuide;
[self.view.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
[self.view.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
[self.view.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
[self.view.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor].active = YES;
}
But, when I try to run the application connecting to iPhone XR iOS Simulator device, the above code is not working. The view is not getting started after the safe area in landscape mode instead it is getting started from the safe area itself due to which the text is getting cut off
Am I missing anything? Can you please suggest on what needs to be done so that the safe area can be implemented?
What UI element are you trying to layout in your view? You mention text is getting cut off. The code you provided is setting the anchor from the view to itself and not to the UI element. Update your code to set the anchors of the UI element to the view.
UITextField *textField; // Your text UI element
UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
[textField.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
[textField.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
[textField.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[textField.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
I'd recommend you to use
constraintEqualToSystemSpacingAfterAnchor: multiplier:
and
constraintEqualToSystemSpacingBelowAnchor: multiplier:
instead of
constraintEqualToAnchor:
Also I'd recommend you to try setting translatesAutoresizingMaskIntoConstraints to NO manually:
textField.translatesAutoresizingMaskIntoConstraints = NO;
The full code:
UITextField *textField = [[UITextField alloc] init];
textField.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:textField];
if (#available(iOS 11, *)) {
UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
[textField.leadingAnchor constraintEqualToSystemSpacingAfterAnchor:guide.leadingAnchor
multiplier:1.0].active = YES;
[textField.trailingAnchor constraintEqualToSystemSpacingAfterAnchor:guide.trailingAnchor
multiplier:-1.0].active = YES;
[textField.topAnchor constraintEqualToSystemSpacingBelowAnchor:guide.topAnchor
multiplier:1.0].active = YES;
[textField.bottomAnchor constraintEqualToSystemSpacingBelowAnchor:guide.bottomAnchor
multiplier:1.0].active = YES;
} else {
// ...
}
This is the code that is provided from FB but this ad is placed on the top banner and I would like it fit to the bottom banner. Is there an easy fix to change the location for the FB ads? Also, the implementation is working and test ads are running.
-(void)viewDidLoad
{
[super viewDidLoad];
FBAdView *adView = [[FBAdView alloc] initWithPlacementID:YOUR_PLACEMENT_ID
adSize:kFBAdSizeHeight50Banner
rootViewController:self];
[adView loadAd];
[self.view addSubview:adView];
}
You can move the banner by changing its frame like this:
FBAdView *adView = [[FBAdView alloc] initWithPlacementID:YOUR_PLACEMENT_ID
adSize:kFBAdSizeHeight50Banner
rootViewController:self];
adView.frame = CGRectMake(0, self.view.frame.size.height-adView.frame.size.height, adView.frame.size.width, adView.frame.size.height);
[adView loadAd];
[self.view addSubview:adView];
I am creating an app using Phonegap and Onsen UI. I am able to show the Admob ads using this code:
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
// Black base color for background matches the native apps
theWebView.backgroundColor = [UIColor blackColor];
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
return [super webViewDidFinishLoad:theWebView];
}
However, if I use iframe, the ads will not showing. Why this happened and how to solve it?
I put an Admob banner at the bottom centre of screen using the following code:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
However, in iPad, I plan to put different ads on another location. Therefore, I need to hide this ads in iPad. Is it possible for me to do this?
try this
bannerView_.hidden=true;
You do not need to make more than one banner view. The easier solution is using the same banner view but checking the current device idiom before positioning the view. For example here is the code you provided modified to check which device the user is on:
- (void) webViewDidFinishLoad: (UIWebView*) webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Change the banner's center/origin here for the iPad.
}
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
i have added all the required files and frameworks for integrating admob. The problem is that it does not add the subview to the viewcontroller.
My viewcontroller consist of a navigation bar and a UITableView. What i want to achieve is to have a admob in the button of my viewcontroller. How can i achieve this.
At the moment i'm using following code, which is not doing anything and not giving me any errors:
bannerView_.adUnitID = #"Banner Key";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
Perhaps you need to set the frame of the banner as well?
1 must set the frame of ad banner so that it can be displayed:
m_bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
m_bannerView.adUnitID = #"AD Unit ID";
m_bannerView.rootViewController = self;
m_bannerView.delegate = self;
m_bannerView.frame = CGRectMake(0, 64, 320, 50); // 64 = height of navigation bar
GADRequest *request = [GADRequest request];
[self.view addSubview:m_bannerView];
[m_bannerView loadRequest:request];
You can set the origin points of the adBanner like below.
self.adBanner = [[GADBannerView alloc]initWithAdSize:kGADAdSizeBanner
origin:CGPointMake(0,31)];
self.adBanner.adUnitID = #"YOUR_KEY";
self.adBanner.delegate = self;
self.adBanner.rootViewController = self;
[self.view addSubview:self.adBanner];
[self.adBanner loadRequest:[self request]];