I'm using AdMob to display ads in my app. The problem is i cant seem to hug to the bottom always.
How can i make the bannerView_ stay at the bottom when people are scrolling?
This is my banner frame:
[bannerView_ setFrame:CGRectMake(0.0, [[UIScreen mainScreen] bounds].size.height-bannerView_.frame.size.height, 320, 50)];
My basic suggestion is.
First create scrollView and add all your controllers on it and add scrollView in self.view as subView.
Then after ( over scrollView) create UIView (name is bannerView) and also add this view in self.view as subView.
So basically this bannerView is overlapping on the scrollView and it stay visible when you scroll up/down.
You can add this bannerView_ to UIWindow. i.e to [[UIApplication sharedApplication].windows objectAtIndex:0]; .
By doing so bannerView_ will be added and will be independent of other views.
This will be helpful in case you want to present your ad on different view controllers and don't want to handle its interference with the base view of different view controllers.
You may follow my code in which banner will be stick at bottom
GADBannerView *bannerView_ = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait] autorelease];
bannerView_.adUnitID = AdMob_ID;
bannerView_.rootViewController = self;
UIView *bannerView=[[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height - bannerView_.frame.size.height, self.view.frame.size.width, bannerView_.frame.size.height)];
bannerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[bannerView addSubview:bannerView_];
[self.view addSubview:bannerView];
[self.view bringSubviewToFront:bannerView];
[bannerView release];
[bannerView_ loadRequest:[GADRequest request]];
Related
I am using the following code to create a view and put it on top:
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
CGRect viewRect = mainWindow.frame;
topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[mainWindow addSubview:topView];
It works perfectly but my problem is if I write anything one the view(like using a Label) and my device is in a landscape position, the text is in vertical position. I've attached a picture to make it more clear. Is there any way to fix it?
EDIT: if I use UIApplication.sharedApplication.keyWindow.rootViewController instead of mainWindow, I will get this:
See this answer:
View on top of everything: UIWindow subview VS UIViewController subview
Basically, only the first subview of the main window gets rotation events, so you have to do it some other way.
There's no way to fix it without adding the given view to a View Controller (UIWindow was never meant to handle rotation, and has no logic to do so).
The wonky view rotation results you're experiencing are actually the result of UIView's default autoresizingMask's
topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[topViewsetAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
I'm trying to show an onscreen tutorial (like a picture with hints) in my viewController. I only know how to "open" a UIView with its drawRect method, where my paint code is inside, from the AppDelegate with:
BannerView *view = [[BannerView alloc] initWithFrame:self.window.frame];
[self.window addSubview:view];
[self.window makeKeyAndVisible];
Is it possible to activate the UIView (BannerView) by a button from inside a ViewController?
Thank you very much!
Simply use this code:
BannerView *view = [[BannerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:view];
in your button action method (if it is defined in your controller class). See this S.O. question to know how to create a button programmatically. Otherwise, you can use interface builder for that.
Using self.view.bounds in initWithFrame will make your banner view as large as the controller's view (which could be smaller than the display).
I believe what you are looking for is adding an event handler to a button on the view controller that will create the view and add the view by using something like
BannerView *view = [[BannerView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:view];
where self is the view controller.
I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.
I want to change the existing view in a UIViewController to a new view. The new view contains the old view and a little banner view.
Doing this fairly simple change leaves me with a black view.
My code looks like this
UIView *existingView = self.view;
UIView *newView = [[UIView alloc] initWithFrame:existingView.frame];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 50), 320, 50)];
CGRect existingViewFrame = existingView.frame;
existingViewFrame.size.height -= 50;
existingView.frame = existingViewFrame;
[newView addSubview:existingView];
[newView addSubview:bannerView];
self.view = newView;
However when switch Tabs and come back to the view which changed the view is shown just like I want. I guess I need to set a flag or something to tell the controller to redraw it's (new) view.
Edit
I wrote an simple example for this problem. You can find it on GitHub: https://github.com/Oemera/ChangeView
You did not say where you do this. It may be that you need to save the original view's super view, then add the new view to that views subViews array. I'm betting that is the problem.
Why does the header and footer of a tableview always stays on top of the view hierarchy, but not the cells of the table?
Here is what I got:
A table view with custom cells, a footer and a header
A Navigationbar with a menu-Button on the right
When the user taps the menu-button, a semi-transparent menu (UIView) fades in from the top, but not over the navigationbar, only over the view of my tableviewcontroller
When the user tabs a menu-button, the menu slides back to (0,-menuHeight)
But when the menu is over the header, this menu-region is behind my header
I anchored the menu on the view of my tableviewcontroller, beacause I want the navigationbar allways be visible. the solution to anchor it on the navigationbar solves the problem with the header-view, but covers the navigationbar.
Has anyone an idea how to solve this problem? Why are the cells painted correctly?
You should probably not interfere directly with the internal view hierarchy of UITableView. Apple is free to order a table view's subviews as they see fit (and change it in future releases).
Instead, place the table view and your menu view into a common container view and make the latter the main view of your view controller. That way, you can be certain that your menu view will always be above the table view.
I changed my design, but now I do not get touch-events in my table.
But when I add this method - I see, that I'm touching the tableviewcontroller...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"ShieldingViewController received touch");
[self.buttonMenu shieldingViewTouched];
}
Here is what I changed:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0;
UIView *rootView = [[UIView alloc] initWithFrame:viewFrame];
rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Setup the table view
UITableView *newTableView = [[UITableView alloc] initWithFrame:viewFrame style:self.tableView.style];
newTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
newTableView.backgroundColor = [UIColor darkGrayColor];
UIView *menuLayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
self.tableView = newTableView;
[rootView addSubview:self.tableView];
[rootView addSubview: menuLayerView];
self.view = rootView;
[[CustomNavigationController instance] setCurrentViewForMenu: menuLayerView];
[[CustomNavigationController instance] showMenuInNavigationBarForController:self];
[newTableView release];
[menuLayerView release];
[rootView release];
}