How to make UITableview exact height and removing unwanted white space footer? - ios

I have an following issue I am having one button on main view if i was click on this button add subview on the main view as tableview here is the code
ViewController *settings = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
settings.delegate=self;
[settings.view setFrame:CGRectMake(0, 30, self.view.frame.size.width,250)];
[self.view addSubview:settings.view];
after i was click on the button get the subview as tableview but rest of them having an whitespace.
then how to remove the remaining whitespace.

At first sight, it looks that you have set wrong height of your ViewController's view i.e., 250. Make sure the tableView inside the view have the same height as your are assigning in setFrame function here:
ViewController *settings = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
settings.delegate=self;
[settings.view setFrame:CGRectMake(0, 30, self.view.frame.size.width, HeightOfYourTableView)];
[self.view addSubview:settings.view];
Hope it helps!

You can simply calculate the height of your table view by multiplying the number of items by the height of the cell, let's say 44 by default.
But regarding the white background, you can do it from the Interface Builder or inside the code like this: (of course I am assuming that inside ViewController, you have access to the table view using some variable)
youTableView.backgroundColor = [UIColor clearColor];

Related

Adding a new view on top of an existing view

I want to add a view called anotherView on top of self.view. But I don't want anotherView to be a subview. Is this possible?
I want anotherView to be at the top of the page and push the contents of self.view down accordingly (without having to change the y or height values.
I have the following and it doesn't work:
UIView *anotherView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 80)];
anotherView.backgroundColor = [UIColor yellowColor];
[self.view insertSubview:anotherView aboveSubview:self.view];
The box is simply on top of the existing view.
Any visible view is subview of some other view or window.
But you can add anotherView to
self.view.superview
or even
self.view.window
or create new UIWindow object and add your subview to this window
Have you tried to present anotherView modally? That way you can make it to appear on top of everything being a completly new view controller. Ex:
[self presentModalViewController:anotherView animated:YES];
assuming of course anotherView is a subclass of UIViewController and has its own View property. Because if anotherView is just subclass of UIView then you can't present it without being a subview of the current View Controller.
EDIT:
Oh yes, you can just pass a nil to the completion block:
[self presentViewController:anotherView animated:YES completion:nil];

ios - UIView to cover full screen, to cover the tabs from a UITabBarController

I have a UITabBarController and I want to add a UIView as a subview but I want that view to cover the whole screen including the tabs on the bottom. All attempts I have done result in the view cover everything except the tabs on the bottom.
Not sure what you have tried but, if you are trying to add the view from a UIViewController that is inside the UITabBarController then you should use:
UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
self.tabBarController.view.frame.size.width,
self.tabBarController.view.frame.size.height)];
[self.tabBarController.view addSubview:coverView];
//and maybe [self.tabBarController.view bringSubviewToFront:coverView];

Add clickable and fixed subview to UITableViewController?

I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:
[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];
The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.
Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?
Thank you in advice!
Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!
First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.
But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:
As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)
So, my code is now looking like this:
#synthesize tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
tableView = (UITableView *)self.view;
}
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
[self resizeTableToFitBanner];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bannerView];
// some other code
}
BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:
https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html
I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.

How do I programmatically display a UISegmentedControl?

I'm looking at an iPhone application, with several ViewControllers controlled by a UINavigationController.
Somewhere deep down one of the ViewControllers is creating a view, and the view is trying to pop a
UISegmentedControl *segmentedControl
up on top of itself like so:
[self.window addSubview:segmentedControl];
"self" is a specialized form of UIView, and the control's frame is set
segmentedControl.frame = someFrame;
For some reason, the control is not showing up on top of the current view. Should this work, and, if it should, what are the most likely causes of trouble?
When you say self is a specialized form of UIView I'm guessing you are referring to a UIView custom subclass.
If you are adding the UISegmentedControl from within this view, you want to do the following:
[self addSubview:segmentedControl];
You set it's frame relative to it's superview, so if you want it on the top left corner you would do:
segmentedControl.frame = CGRectMake(0.0f, 0.0f, someWidth, someHeight);

Two controllers inside of a UIPopoverController: having issues with frames

I have a UIPopoverController with two view controllers inside of it. I'm building it like so:
CommentsPopoverController *commentsPopoverController = [[CommentsPopoverController alloc] init];
self.delegate = commentsPopoverController;
commentsPopoverController.navigationItem.title = #"Comments";
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:commentsPopoverController];
popover = [[UIPopoverController alloc] initWithContentViewController:navController];
Inside my commentsPopoverController I have this:
commentsViewController = [[CommentsViewController alloc] init];
commentsViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
addCommentsViewController = [[AddCommentViewController alloc] init];
addCommentsViewController.view.frame = CGRectMake(0, commentsViewController.view.bounds.size.height - 200, 320, 346);
addCommentsViewController.view.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:addCommentsViewController.view];
[self.view addSubview:commentsViewController.view];
So when I first load the popover, the addCommentsViewController is hidden by the commentsViewController. When I reveal it, it looks like this:
So far so good. The problem I'm having is that from here, when the user rotates the device or shows the keyboard or shows the keyboard THEN rotates the device things start to get out of whack. The top view controller (commentsViewController) which is a UITableView always does the right thing no matter what the orientation is or whether or not the keyboard is showing. But the bottom view controller (addCommentsViewController) doesn't automatically change it's origin.y to stay directly under the top view controller.
So I've basically had to hack the crap out of my code to keep the addCommentsViewController directly under the commentsViewController by constantly calculating the height of the top view controller so that I could adjust the bottom view controller's origin.y. This involved dropping in NSNotifications for the keyboard's show/hide state and for the device's orientation and constantly recalculating. Very hackish and ugly.
So my question (finally) is: Is there an easier way of controlling these views or am I stuck hacking it the way I did?
To handle rotation, there are two appropriate techniques. One is that you make CommentsPopoverController's view a subclass of UIView that overrides layoutSubviews to lay out your two views properly. The other is that you define viewDidLayoutSubviews on CommentsPopoverController to lay out your views. If you lay out your views in either of these methods, you shouldn't have to subscribe to rotation notifications.
As for moving your view out from under the keyboard, that is discussed in the Text, Web, and Editing Programming Guide for iOS. Part of the technique involves subscribing to keyboard will show/did hide notifications.
I assume your CommentsViewController is a subclass of UITableViewController, because UITableViewController takes care of adjusting the table view when the keyboard is hidden or shown.

Resources