I'm trying to display a UIButton over an AdmobBanner like the example below (the black X obviously). I looked for this option in the Admob SDK but couldn't find anything.
I've tried selecting the button->Editor->Arrange->Send to Front with no results.
Help is much appreciated.
This is the code I use to show my banner:
CGPoint origin = CGPointMake(0.0,0.0);
// Use predefined GADAdSize constants to define the GADBannerView.
self.adBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
// Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID
// before compiling.
self.adBanner.adUnitID = #"a150xxxxxxxxxxx";
self.adBanner.delegate = self;
[self.adBanner setRootViewController:self];
[self.view addSubview:self.adBanner];
self.adBanner.center =
CGPointMake(self.view.center.x, self.adBanner.center.y);
[self.adBanner loadRequest:[self createRequest]];
Im assuming you're using interface builder here, and that both the AdMob view and your button are subviews of the same parent view. If that is the case, just make sure the button comes before the AdMob view in the list of subviews in the Objects pane on the left.
Another option is to make the following call in viewDidLoad:
[self.view bringSubviewToFront:button]
Make sure you do this after inserting the AdMob view, if you're doing it programmatically
Related
I can't figure out how to change my AdMob banner's background color when the ad doesn't fit. Is this possible? The code below wouldn't work for me.
self.ad.backgroundColor= [UIColor whiteColor];
This is actually achieved through AdMob's dashboard.
Go to AdMob.com
Select Monetize on the top toolbar
Select the application on the left side bar that you want to change the background color for
Select the Ad Unit of the banner
In the Text ad style drop down menu select Customized
Select Background color and change it to whichever color you desire
Select Save and you're all done
It may take a few minutes for changes to appear in your application.
You could add your GADBannerView to another UIView that you set the backgroundColor of. This would require using AdMob's default sizes to make sure that image banners fit properly. The down fall of this is that text based ads will be restricted to this size also instead of filling the entire ad area.
For example:
#import "ViewController.h"
#import GoogleMobileAds;
#define ADUNIT_ID #"yourAdUnitID"
#interface ViewController () <GADBannerViewDelegate> {
GADBannerView *admobBanner;
UIView *backgroundView;
}
#end
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
// Create our AdMob banner
admobBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
admobBanner.adUnitID = ADUNIT_ID;
admobBanner.rootViewController = self;
admobBanner.delegate = self;
[admobBanner loadRequest:[GADRequest request]];
// Create a view to put our AdMob banner in
backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0,
self.view.frame.size.height - admobBanner.frame.size.height,
self.view.frame.size.width,
admobBanner.frame.size.height)];
// Hide view until we have an ad
backgroundView.alpha = 0.0;
// Set to color you require
backgroundView.backgroundColor = [UIColor redColor];
// Add our views
[self.view addSubview:backgroundView];
[backgroundView addSubview:admobBanner];
// Center our AdMob banner in our view
admobBanner.center = [backgroundView convertPoint:backgroundView.center fromView:backgroundView.superview];
}
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
NSLog(#"adViewDidReceiveAd");
[UIView animateWithDuration:0.5 animations:^{
backgroundView.alpha = 1.0;
}];
}
-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(#"adView:didFailToReceiveAdWithError: %#", [error localizedDescription]);
[UIView animateWithDuration:0.5 animations:^{
backgroundView.alpha = 0.0;
}];
}
iPhone / iPad
You should insert the ad in a container view.
Then, in the delegate that is called when the ad is loaded adjust the container width to be the same size as the ad view
You need to start by figuring out what views compose the ad view. Probably you are changing the view's color, but it has another view on top of it that contains the ad, and total blocks the parent view.
Start by placing a breakpoint in your code (I often place it in -viewDidAppear:) and once in there, type this in the debugger to see the subviews of the ad view:
po self.ad.subviews
My guess is that you'll see one subview that stretches the full length of the main window. It may even be a UIImageView that contains the ad image. But you can continue to look at the subviews of the subviews, either in the debugger or by adding code in -viewDidAppear:.
This is probably view you want to mess with. In -viewDidAppear:, try adding code to color the subview's background instead:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *adSubview = self.ad.subviews.firstObject;
adSubview.backgroundColor = [UIColor whiteColor];
}
This code should be safe even if the ad or its subviews property returns nil.
I think you should fill rect of UIView same Admob size.
bannerView = GADBannerView(frame: rectBanner)
bannerView.adUnitID = "xxxxxxxxxxxxxxxxxx"
bannerView.rootViewController = self
frame size(rectBanner) should be match with Admob Size.
I'm working on example from "iOS Programming Cookbook", based on iOS 7. I need to create UITextField and add it to ViewController.
In ViewController.m I have:
- (void) viewDidLoad {
[super viewDidLoad];
[self createField];
}
- (void) createField {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = #"Enter text to share";
self.textField.delegate = self;
[self.view addSubview:self.textField];
}
On screenshot from book textField appears in the middle of screen's width under the status bar, but when I run it textField appears on the top left corner of screen behind the status bar.
Maybe the problem is, that I run app on iPhone 6 simulator with iOS 8. But I don't know how to solve it.
Thanks!
Using self.textField.translatesAutoresizingMaskIntoConstraints = NO; is pretty much telling the object that it doesn't really care about the frame but relies more on the constraints that you give it. Setting it to YES takes the frame and automatically applies constraints to it to mimic the frame that you give it.
For example it would apply the constraints to have the frame appear at: CGRectMake(20.0f, 35.0f, 280.0f, 30.0f). When setting it to NO use NSLayoutConstraint and create the constraints programatically.
But in your case just remove the line
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
because it is set to YES by default.
Can I have some UIView which will always appear on top in iOS?
There are lots of addSubview in my project but I need to have one small view which will always appear. SO is there any other option than
[self.view bringSubViewToFront:myView];
Thanks
One more option (especially if you want to overlap several screens, with logo for example) - separate UIWindow. Use windowLevel to set the level of new window.
UILabel *devLabel = [UILabel new];
devLabel.text = #" DEV ";
devLabel.font = [UIFont systemFontOfSize:10];
devLabel.textColor = [UIColor grayColor];
[devLabel sizeToFit];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
static UIWindow *notificationWindow;
notificationWindow = [[UIWindow alloc] initWithFrame:
CGRectMake(screenSize.width - devLabel.width, screenSize.height - devLabel.height,
devLabel.width, devLabel.height)];
notificationWindow.backgroundColor = [UIColor clearColor];
notificationWindow.userInteractionEnabled = NO;
notificationWindow.windowLevel = UIWindowLevelStatusBar;
notificationWindow.rootViewController = [UIViewController new];
[notificationWindow.rootViewController.view addSubview:devLabel];
notificationWindow.hidden = NO;
Another option is set layer.zPosition of your UIView.
You need to add
#import <QuartzCore/QuartzCore.h>
Framework to your .m file.
And set such like
myCustomView.layer.zPosition = 101;// set maximum value as per your requirement.
For more information about layer.zPosition read this documentation.
Discussion
The default value of this property is 0. Changing the value of this property changes the the front-to-back ordering of layers onscreen. This can affect the visibility of layers whose frame rectangles overlap.
The other option is to add other subviews below this always-on-top subview. For example:
[self.view insertSubview:subview belowSubview:_topSubview];
There's no solution with Interface Builder if you search for this kind. It should be done programmatically. If you don't want to use bringSubviewToFront: everytime, just insert other subviews below this one.
Many times your view did not appear in viewDidLoad or, if your view comes from parentViewController (for example in many transitions like modal segue..) your can see parentViewController only in viewDidAppear so:
Try to put bringSubviewToFront in :
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.view bringSubViewToFront:myView];
// or if your view is attached in parentViewController
[self.parentViewController.view bringSubViewToFront:myView];
}
Good luck!
Hi I have a UIScrollView inside of a UIView. I have tried to use code snippets that I found online but they simply don't change anything. Also they are mostly for an image or custom view done within UIView, whereas in my case I have an array of programatically created UILabels. I have tried to change boundary values as well, it simply does not do anything. This is basically how I establish the size of it within viewDidAppear:
[scrollView setContentSize:CGSizeMake([screenView getWidth], [screenView getHeight])];
scrollView.showsHorizontalScrollIndicator = true;
scrollView.showsVerticalScrollIndicator = true;
screenView is a UIView variable.
This is the settings that I use(also in viewDidAppear):
doubleTapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapResponse:)];
[doubleTapRecogniser addTarget:self action:#selector(doubleTapResponse:)];
doubleTapRecogniser.delegate = self;
doubleTapRecogniser.numberOfTapsRequired = 2;
[self.scrollView addGestureRecognizer:doubleTapRecogniser];
This is how I implemented my double tap method:
- (void) doubleTapResponse:(UITapGestureRecognizer *)recogniser
{
CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
[self.scrollView setZoomScale:newZoomScale animated:YES];
}
When I use NSLog messages within my doubleTapResponse, I can get responses from my console. However it does not do anything. What could be the problem?I am using iOS6.1
The error clearly says that the run time searched for a method named doubleTapResponse in the scrollview class you are using. Even if changing the target to self doesn't work, its the method definition place you have to change either the scrollview or the viewcontroller.
[doubleTapRecogniser addTarget:scrollView action:#selector(doubleTapResponse:)];
should be
[doubleTapRecogniser addTarget:self action:#selector(doubleTapResponse:)];
because the scrollview does not know what that method doubleTapResponse is.
Currently it is throwing an exception because it is trying to call the target of the UISCrollView with your doubleTapResponse method, you must add the target of self, and implement this method yourself. In here goes the logic for zooming I presume.
You must also define: doubleTapResponse in your viewcontroller (or class that you are using)
see this for more info:
Ray Wenderlich guide
In order to zoom please look at the following article: QUESTION
I'm trying to resize my tableView when I am have an AdMob view at the bottom of my screen. I've tried a couple things: Change UITableView height dynamically and Resizing UITableView When Displaying AdWhirl Ads Across Multiple Views and Change size of UIViewTable to accommodate for AdWhirl Ad but none of those have worked. By not worked, I mean NOTHING happens. The view is EXACTLY the same as it was before I tried those changes. So you know, this tableView is nested inside of a ViewController. Here is the layout:
Here is the last thing I've tried:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO];
#ifdef FREERECORDER
CGPoint origin = CGPointMake(0.0,self.view.frame.size.height - 90 - CGSizeFromGADAdSize(kGADAdSizeBanner).height);
gBannerView =[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
//this is where I'm attempting to resize
CGRect tableFrame = self->tableView.frame;
tableFrame.size.height = self->tableView.frame.size.height - 500;
self->tableView.frame = tableFrame;
gBannerView.adUnitID = #"MY_AD_ID";
gBannerView.rootViewController = self;
[self.view addSubview:gBannerView];
GADRequest *request = [GADRequest request];
// Make the request for a test ad. Put in an identifier for
// the simulator as well as any devices you want to receive test ads.
request.testDevices = [NSArray arrayWithObjects:
#" MY_TEST_ID",
nil];
[gBannerView loadRequest:[GADRequest request]];
#endif
self.title = #"All Root Beers";
RootBeerFeedParser* rfp = [[RootBeerFeedParser alloc]init];
rootBeerList = [rfp getCoreDataRootBeers];
self.tabBar.delegate = self;
[self->tableView reloadData];
}
This is the result:
![enter image description here][2]
What it doesn't show, is that the last cell in the tableview is covered by that advertisement and I'm trying to fix that.
If you want to resize tableView by updating its frame, then you should turn off the autolayout mode. With autolayout you should update constrains but not frames.
You should implement updateConstraints (http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/updateConstraints) in order to specify the new requirement.
You may also need to call setNeedsUpdateConstraints at some point in time (if you need to show and hide the ad view).