Adding multiple views to a container view - ios

I am looking at adding numerous TableViews to an existing UI View Controller.
I have seen https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html which seems to be the way I'm meant to go.
However, my question is can I embed more than one TableView to a single ContainerView?
My app does a variety of tests for which there are results. After each test completes I want to add a TableView so they appear underneath each other neatly.
Is this possible?

Yes you can. You can do two things:
ONE View Controller - MANY Table Views (as subview of the root view)
or
ONE Container Controller - MANY ChildViewControllers using VC containment (and each has one Table View )

This is what you could do in ViewDidLoad for a UIViewController subclass.
// Do any additional setup after loading the view, typically from a nib.
CGRect frame = self.view.frame;
// this container shall hold the two tables that I am going to add later
// container shall share the same size as view controller's "view"
UIView *container = [[UIView alloc] initWithFrame:frame];
frame.size.height = frame.size.height / 2; // I want to fit two table view vertically to cover the container view
UITableView *tableView1 = [[UITableView alloc] initWithFrame:frame];
tableView1.backgroundColor = [UIColor redColor];
// ...
// ...
// Assign more table view properties here
// ...
// ...
[container addSubview:tableView1];
frame.origin.y = frame.size.height; // update coordinates for the second table view
UITableView *tableView2 = [[UITableView alloc] initWithFrame:frame];
tableView2.backgroundColor = [UIColor greenColor];
// ...
// ...
// Assign more table view properties here
// ...
// ...
[container addSubview:tableView2];
[self.view addSubview:container];
you shall have to set Delegates for all tableviews to make them work properly, but this should give you something to get going. (I have added some different background colors to distinguish between both the tableviews)

Related

ios uitableview bounce went wrong

I implemented a horizontal table view and it looks like this
The category bar, Dining Shopping something, is a horizontal table view and here is the implementation code.
LogInfo(#"Show Category Table start");
// add categories to be subview controller
self.categoriesTable = [[UITableView alloc] init];
self.categoriesTable.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
[self.categoriesTable setFrame:CGRectMake(0, 64, SCREENWIDTH, 44)];
self.categoriesTable.showsVerticalScrollIndicator = NO;
self.categoriesTable.showsHorizontalScrollIndicator = NO;
self.categoriesTable.pagingEnabled = YES;
self.categoriesTable.delegate = self;
self.categoriesTable.dataSource = self;
self.categoriesTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.superView addSubview:self.categoriesTable];
LogInfo(#"Show Category Table Finished");
self.categoriesTable.backgroundColor= [UIColor redColor];
It works as expect but if I change view, for example, I click any button to go to other view and go back to this view. The tableview looks like this
This problem also happpens even if I disable the bounce effect of the table view. Does anyone know how to solve this problem? Thanks!
Rather than applying a transform to the table to make it horizontal, use a collectionView with a horizontal layout.
Edit: If you want to continue using your current setup, try disabling automaticallyAdjustsScrollViewInsets on your view controller.
self.automaticallyAdjustsScrollViewInsets = NO
Edit 2: If you're curious, since iOS 7 every view controller looks at its topmost/first scroll view and adjusts its contentInsets to compensate for the navigation bar transparency which is enabled by default. In this case of course, such behaviour isn't desired at all.

view from xib won't autoresize

I have a custom split view controller, designed to work as much like apple's as possible.
I use this split view in three different places. Two of these places, the master is a view simply a UIViewController with a UIView attached, and created programmatically.
The third example however is a UIViewController with a XIB.
The problem is that the first two split views adjust their widths correctly. However the screen with the XIB does not. I'm not doing anything fancy, just trying to set the content view of my master with a view that is passed in to it. And setting the autoresizing masks as such:
// create container views
masterView = [[VS_MasterView alloc] init];
masterView.frame = CGRectMake(0, 0, masterViewWidth, self.view.frame.size.height);
masterView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:masterView];
// get the views
UIViewController *masterViewController = (UIViewController *)[_viewControllers objectAtIndex:0];
UIViewController *detailViewController = (UIViewController *)[_viewControllers objectAtIndex:1];
// set their bounds
[masterViewController willMoveToParentViewController:self];
[self addChildViewController:masterViewController];
[masterViewController didMoveToParentViewController:self];
masterViewController.view.frame = masterView.bounds;
masterViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[masterView addSubview:masterViewController.view];
is there some sort of special case with XIB files and resizing them in this situation? I don't feel like there should be.
thanks

How can I add a table view controller to an existing storyboard view controller?

I'm creating a landscape iPad app for iOS 5.1 that should have two table views embedded into a view controller of my storyboard. What I would like to be able to do, is drag a table view controller onto the view controller in my storyboard. But of course, Xcode does not allow this. I can drag a table view and get its data hooked up and it works properly, but then I cannot push a new view controller to replace that table when a row is selected.
I cannot use the 'Editor > embed in > navigation controller' trick, because then the entire storyboard view controller (which contains my two table views) is embedded. That is not what I want.
There must be a way to do this programmatically, but I can't seem to get the right combo of voodoo and science to make it work.
I have tried to create a custom container view to hold my tableViewController, but the table isn't showing up.
Any thoughts?
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(68, 187, 402, 474);
_containerView = [[UIView alloc] initWithFrame:frame];
_containerView.backgroundColor = [UIColor redColor];
[self.view addSubview:_containerView];
categoryController = [[UITableViewController alloc] init];
categoryTable = [[UITableView alloc] init];
categoryTable.delegate = self;
categoryTable.dataSource = self;
[categoryController.view addSubview:categoryTable];
[_containerView addSubview:categoryController.view];
}
What you need is Container Views
Here are some tutorials that can help you:
http://www.cocoanetics.com/2012/04/containing-viewcontrollers/
http://weblog.invasivecode.com/post/12383262201/container-view-controllers-part-i-one-of-the
https://developer.apple.com/videos/wwdc/2011/?id=102

Replacing self.view with new UIView shows black view

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.

Adding viewController views to UIScrollView

Despite the number of similar posts on this, I still am having trouble making this work correctly.
I have a two UIViewControllers which I have designed in IB with UILabels and imageviews, etc.
I would like to add these to a scroll view so they can be paged between.
I have outlets defined connecting each of the scrollViewcontrollers with their elements in IB, however, the controllers themselves, I am programatically creating from within the master view controller in viewDidLoad. After creating them, I assign some values from the master view controller than add it as a subview to the scrollViewController. However nothing displays. From the debugging i have done, when I set a break point after assigning the values, I can see that the elements are nil. They should be loaded from the xib file, but it does not seem to be read. Even as a test, I tried initializing one of the elements (eg. UILabel) and setting it to that sub view controller, but it nothing would display. Here is some code to explain a bit better.
In the master view controller
detailControllerA *tempA = [[detailControllerA alloc] initWithNibName:#"detailOverviewA" bundle:nil];
self.overviewA = tempA;
[tempA release];
self.overviewA.someLabel.text = #"Some Text";
detailScrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight);
detailScrollView.pagingEnabled = YES;
[detailScrollView addSubview:self.overviewA.view];
In the detailControllerA implementation I set the frame in loadView:
-(void)loadView {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
self.view = view;
[view release];
}
I also have the labels, etc defined with IBOutlets in detailControllerA.h and implemented to the elements in the xib file.
Any ideas why the xib is not loading correctly when created programatically?
Try in this way:
NSUInteger scrollContentCount = 0;
for (NSUInteger arrayIndex = 0;
arrayIndex < [contents count];
arrayIndex++) {
// set scorllview properties
CGRect frame;
frame.origin.x = self.mScrollView.frame.size.width * arrayIndex;
frame.origin.y = 0;
frame.size = self.mScrollView.frame.size;
myScrollView.autoresizingMask = YES;
// alloc - init PODetailsView Controller
myController = [[MyController alloc] initWithNibName:#"MyController" bundle:[NSBundle mainBundle]];
[myController.view setFrame:frame];
// add view in scroll view
[self.myScrollView addSubview:myController.view];
scrollContentCount = scrollContentCount + 1;
}
// set scroll content size
self.myScrollView.contentSize =
CGSizeMake(self.myScrollView.frame.size.width * scrollContentCount,
self.myScrollView.frame.size.height);
}
Don't release the content controller object which is in for-loop.
Set your scrollView contentSize according to your requirement.
Hope this would be helpful to you.
Interestingly enough, the -(void)loadView was the problem.
Since the view is being generated in interface builder (with the frame settings), when loadView fires, it was creating a new view, blowing away the one created in interface builder.

Resources