Screen size minus advertising size - ios

How can I get the screen height minus the height of the ads I'm displaying?
I get my screen size with
[[UIScreen mainScreen] bounds].size;
And I'm showing one banner ad:
self.canDisplayBannerAds = YES;
So the screen height should be something like:
[[UIScreen mainScreen] bounds].size.height - banner.height;
I want to place a object in the exact middle of the screen and the banner isn't showing all the time.

canDisplayBannerAds will show an ad that you can't adopt its delegate what so ever. Also you cant reference the ad view to get frame height or width. I suggest using old fashioned way. I can post a code if you want.

Getting the size of the screen isn't really what you want to do. This is because if the device turns sideways (if you are supporting rotation) or if your view controller does not take up the entire screen (like if your view controller is in a UINavigationController or UITabBarController) your banner will be off the screen.
To place the banner at the bottom of the viewControllers view and place an other view in the middle of a viewControllers view you could write something like this
-(void) viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
CGRect bounds = self.view.bounds; // for a normal view controller {0,0,width,height}
CGRect bannerRect = self.banner.frame;
bannerRect.origin.y = bounds.size.height - bannerRect.size.height; // bottom of the screen
bannerRect.origin.x = (bounds.size.width - bannerRect.size.width)/2.0; // centre horizontally
self.banner.frame = bannerRect;
CGRect objectRect = self.objectView.frame;
objectRect.origin.y = (bounds.size.height - objectRect.size.height)/2.0; // centre vertically
objectRect.origin.x = (bounds.size.width - objectRect.size.width)/2.0; // centre horizontally
self.objectView.frame = objectRect;
}

Related

iOS part of viewcontroller goes black when orientation changes

When I change viewcontroller orientation while it's loading on particular orientation, part of the screen goes blank at the bottom.
Steps to reproduce:
Have tableview(Not necessary I think. could be any view) in a view controller
Push a new viewcontroller which also has a tableview(Not necessary I think. could be any view) during
an action
Rotate the screen from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part.
Press back button to pop the current viewcontroller
Now rotate from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part here as well.
The bottom part of viewcontroller goes black.
I am able to reproduce 7 out of 10 times.
FYI:
My Viewcontroller has only tableview that's all and all cells has autolayout constraints.
Trying to understand why it happened and I would like to fix it.
More details on reproducing the issue:
Basically you can reproduce this issue only if you hold your device in slanting position and either push or pop a view controller and while the page is trying to load , you have to change the orientation then the issue happens.
(Pun intended.... lol )
In other words ,you have to tilt you device as if you are steering the wheel :).
This issue can only happen if you use your device like a steering wheel :)
In your viewWillTransition method call layoutIfNeeded()
Try to reset the layout of TableView after rotation by using this method,
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.layoutIfNeeded()
}
or
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
}
I also experienced the same issue.
My view controller had just one tableview and when we tilt , I mean change the orientation during a navigational push or pop this used to happen.
Try setting the frame
in viewDidLayoutSubviews
-(void)viewDidLayoutSubviews{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
CGRect frame = self.tableView.frame;
// To check if there's any mismatch between the sizes of screen and tableview then set the tableview frame same as screen frame.
if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
self.tableView.frame = CGRectMake(0, 0, width, height);
}
}
or try setting the frame in viewWillTransitionToSize
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
CGRect frame = self.tableView.frame;
if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
self.tableView.frame = CGRectMake(0, 0, width, height);
}
}
FYI:
if setting the frame self.tableView.frame = CGRectMake(0, 0, width, height); doesn't work then you could try [self.tableView layoutIfNeeded]
Set frame again in
viewDidLayoutSubviews
In one view controller(Parent the one that pushes new view controller) setting the frame at viewDidLayoutSubviews solves the issue in another view controller(The child view controller , one that's getting pushed) setting the frame at viewWillTransition fixes the problem :) But still I don't know why there's no universal solution to this problem.

UiView frame not working

I have a UIView,
UIView *topBarView
I am deciding the size i.e. the width and height of topBarView as
CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height*.05);
Now I am setting the frame of topBarView as
topBarView.frame = (CGRect){0, 0,topBarSize};
Im my view controller I have set this condition for rotation
- (BOOL)shouldAutorotate
{
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
When my view controller opens in portrait mode it is all good and as expected the topBarView is placed at the top
But when my view opens in Landscape Left mode the topBarView instead of being on top in Landscape is on the left of the screen i.e same frame as it was in case of portrait
How can I fix this?
As you have set frame your view's origin is (0,0) so it will definitely set in top - left corner in any orientation.
If you want to manage width and height according to orientation then you should use autolayout.
You should set top,leading,trailing and fixed height constraint to that view. so, it will will manage height,width and position with orientation.
Update:(as asked in comment)
Write below code in viewdidload and comment yours
UIView *topBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.5)];
topBarView.backgroundColor = [UIColor orangeColor];
[self.view addSubview: topBarView];
Hope this will help :)

UITableView frame size strange behaviour

I try to set frame size to my table view programmatically after viewDidLoad:
[_tableView setFrame:CGRectMake(0, topHeight,
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height - topHeight)];
Default size is 200x200 with origin Y 100:
Scene hierarchy:
Implementation of UITableView shows when setFrame was called:
#implementation MainTableView
- (void)setFrame:(CGRect)frame {
NSLog(#"setFrame: %f %f", frame.size.width, frame.size.height);
[super setFrame:frame];
}
#end
All is fine but table's frame resizes to default when I press any cell without printing log messages from setFrame. Small explanation video: http://youtu.be/JUW_rHvCS2I
I don't understand why my table size becomes default (200x200) after cell click. Even if I try to set size inside viewWillAppear (after return from cell detail view) it doesn't work.
Any ideas?
I can't really tell why your tableView isn't resizing properly since you haven't provided much code to look around. Though I recommend setting some constraints or resizing rules for the tableView, probably something like this:
http://i.imgur.com/N9fvvIB.png (if not using Auto Layout)
http://i.imgur.com/3Uz8e5e.png (if using Auto Layout)
Now if you really need to make this work only trough code I suggest throwing up some NSLog's containing the tableView's frame width and height before and after setting it up.
Try _tableView.translatesAutoresizingMaskIntoConstraints = NO.

Available screen height with UINavigationBar + UITabBar

So I'm developing an app with a UINavigationBar on top, and a UITabBar on the bottom. And I'm having a hard time trying to figure out what is the real available height for the views.
I've tried calculating the available screen height by calculating the UINavigationBar height and UITabBar height and subtract it from the main screen height, but that is still not the real size.
Here's my failed code for obtaining the available screen height:
CGRect screenRect = [[UIScreen mainScreen] bounds];
_screenHeight = screenRect.size.height;
CGFloat tabBarHeight = _rootViewController.tabBar.frame.size.height;
UINavigationController* navigatior = _rootViewController.viewControllers[0];
CGFloat navigationBarHeight = navigatior.navigationBar.frame.size.height;
CGFloat visibleHeight = _screenHeight - navigationBarHeight - tabBarHeight;
return visibleHeight;
Is there a clean way to know the real available screen size in an UINavigationBar + UITabBar based app??
You don't need to calculate it and it changes depending on the iPhone. Your best bet and easiest path is to use the storyboard and embed both your navigation controller and tab bar directly in there. Once you have done that, you can use auto layout to pin your views to the bars and everything will be sized perfectly for you.

iOS UITableView height issue when 3.5 inch simulator display

A UIViewController with a navigation bar, a toolbar and a admob banner. Below is the code to calculate the height of the UITableView by removing the height of the admob banner, navigation bar and toolbar .
The UITableview height is OK if running on IPhone 5 and 4-inches simulator display.
But if 3.5-inches and non-retina display, the bottom of the UITableView can not be scrolled to.
No matter the 4-inches, 3.5-inches, and non-retina display the height (self.view.bounds) is the same, 548. And the cell height is 41.
For 3.5-inches and non-retina display, if I minus 88(Navigation bar height and toolbar height) after removing the height of admob banner, navigation bar and toolbar height. Then the UITableView scrolls OK. But it not not OK for 4-inches display.
Any comment helps, thanks.
- (void)viewDidLoad{
// admob banner
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// uitableview
CGRect rect = self.view.bounds;
rect.origin.y = bannerView_.frame.size.height;
rect.size.height = rect.size.height - bannerView_.frame.size.height
- (self.navigationController.toolbar.frame.size.height)
- (self.navigationController.navigationBar.frame.size.height);
myTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
[self.view addSubview:myTableView];
}
Try adjusting your UITableView Autoresizing properties programmatically.
something like this
myTableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
update
use this to determine your screen size and set your UITableView frame height programmatically.(Remember that you need to reduce(20) from the table view height because you have toolbar)
- (BOOL)hasFourInchDisplay {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0);
}
Is self.view.bounds changing appropriately on the different device? If not, wherever you set the size of the overall view needs to change.
Also, most of this logic should be in viewWillLayoutSubviews and not viewDidLoad, since views may be resized after they load.
this should work:
rect.size.height = self.view.frame.size.height - bannerView_.frame.size.height
- (self.navigationController.toolbar.frame.size.height)
- (self.navigationController.navigationBar.frame.size.height)-20;
//20 for status bar

Resources