iOS UITableView height issue when 3.5 inch simulator display - ios

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

Related

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 :)

What changes contentOffset when a UIScrollView with contentInset changes its frame from an autoresize?

I have an issue where a scroll view will behave as expected on an iPhone 5 screen (320 points wide) but will have a negative horizontal contentOffset when it's autoresized by its super view to fit an iPhone 6 screen (375 points wide). The reason the offset can be negative is because the view also has a contentInset to the left. But since it was 0 before the automatic resize, I would expect it to still be 0 after.
I am setting up a UIScrollView inside the initWithStyle:reuseIdentifier: method of a UITableViewCell subclass like this:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentSize = self.contentView.bounds.size;
scrollView.contentInset = UIEdgeInsetsMake(0, 100, 0, 0);
[self.contentView addSubview:scrollView];
I can confirm that the contentOffset is (0, 0) at this point (presumably because everything gets autoresized to fit the iPhone 6 screen at a later point in time). I tried probing the value in various methods. In setFrame it's (0, 0) but in the first call to layoutSubviews it's (-55, 0) – presumably because the difference between iPhone 6 and iPhone 5 horizontal display points is 375 - 320 = 55.
Here's a screenshot of what happens on an iPhone 6 screen (in this screenshot I have put additional views inside the scroll view so that it can be seen graphically):

UIScrollView not scrolling properly.

I have a uiscrollview dragged onto the storyboard that is 320x570.
In my viewDidLoad, I have added in
self.scrollView.delegate = self;
self.scrollView.userInteractionEnabled = YES;
self.scrollView.contentSize = self.view.frame.size;
Now when I run the app on the iPhone 5 simulator, my view scrolls but not to the very bottom. When I run the app on the iPhone 4 (smaller screen), it does not scroll at all. What determines how far the scroll should go?
self.scrollView.contentSize must be more than self.view.frame.size to scroll.
Small example for you:
(X is extra place that must be scrollable to see in small screens.)
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height + x)];
In you're case you set height of content same as you're main view so no scroll will be applied.

Screen size minus advertising size

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;
}

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.

Resources