UIView over UITableViewController - ios

I need to place a UIView over UITableViewController, for now i placed it this way
[self.navigationController.view addSubview:searchView];
But problem is that - when i push this UITableViewController by another - my UIView is not pushed away with UITableViewController, but still hang up there, of course i just can remove it with animation, but it looks crapy enough
Is there a way to make that my UIView will be cover another VC together with UITableViewController?
Thanks!

You can't use [self.view addSubview:searchView] as in that case self.view is UITableview only so this will add the searchview on tableview and your view will also scroll with the tableview.
You can use
[self.navigationController.view addSubview:searchView];

UITableViewControllers in fact also have a view property which you can add subviews to. So my advice would be to just send [self.view addSubview:searchView]; inside the table view controller, so that it will be moved alongside the table view, when pushing another controller.

Related

Why Does the TableView Show Under the Navigation Bar

I set my navigation bar to be transparent at the start. I also set a UIlabel at the top of my ViewController if it does not need to be transparent. This works for UIViewController, but if I use UITableViewController, I cannot add a UIlabel at the top of my view, because it is embedded with the UITableViewController and scrolls with the TableView. Below is a sceenshot which looks the TableView.
How do I fix this?
A UITableViewController can only be used if its view is just the table. This means that your view controller can't be a UITableViewController unless the table occupies the whole scene.
To work around this in a legal way, you need to construct a custom parent view controller, like this:
ViewController
child: UITableViewController
The ViewController's view now contains the label and the table view controller's tableView. Problem solved.
You can very easily configure this in the storyboard using a Container View to stand for the table view in the ViewController scene. Indeed, this is probably one of the most important uses of a Container View.
You can add tableview in UIViewController on place of UITableViewController.
In case of UITableViewController use it as fixed header and just uncheck the bounces of UITableViewController in utility area.
Hope this will help you.
you can use this in your viewDidLoad
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}

How to add UITableView inside custom UIView?

I want to create common custom view with uitableview. I can do it using view controller and add subview as follows.
ViewController *vwCon=[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vwCon.view.center=self.view.center;
[self.view addSubview:vwCon.view];
But after scrolling table view app become crash. So how can I add uitableview inside custom UIView?
You shouldn't add a view controller as a subview - in fact Im not sure whether you are allowed to at all. You need to present a view controller modally, or push it as part of a navigation controller. I doubt you really want that though- instead just use a UIView here rather than a view controller. Then you can add that as a subview to another view.
You also mention tableviews though. Have you looked at the storyboard? Its easiest to do all that in there - just drag a UITableview object from the objects list on the right hand side, into the main view controller.

How to add a UIViewController's view as a subview to a UITableViewCell?

I have a reusable UIViewController subclass (an audio/video player, let's call it MediaController). It works ok when I add it to some other view controller as a child view controller, however my requirement is to also add it in a UITableViewCell subclass. Since -addChildViewController: is a method of UIViewController, I'm adding my view to the cell like that:
self.mediaController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.mediaController.view];
(containerView is just a placeholder view in the cell's view hierarchy).
However, this causes problems, first because MediaController is having some logic in -viewWillAppear and -viewWillDisappear (that of course never get called) and second because it seems that autolayout does not work properly when MediaController's view is added to the cell.
Do you think I have some other option (maybe use the UITableViewController that owns the cell as a container?) or I will not be able to use this MediaController at all?
This question is the most relevant when I search, but it still doesn't solve my problem.
Thanks!
One thing I might try if it's possible is to have a UITableViewController that has static cells.
If you're using a UIStoryboard drag and drop a UITableViewController and change the content to Static Cells then in the cell you want to have your MediaController drop a Container View into that cell. Then drag and drop from that Container View to your MediaController and setup an embed segue.
The appropriate viewLifecycle methods should be called when displaying.
Here is the UIStoryboard setup
The other answer from aahrens did not work for me since I have a complicated table view and I was not using storyboards from the beginning.
What I ended up doing was to pass a weak reference of the UIViewController to the cell, so that I can make the "normal" call to -addChildViewController:. Ugly, but works fine.

UIScrollView and Storyboard

I am testing out something I would like to have in my app. I have a UiViewController in a storyboard that has a UIScrollView - I now want to add other viewControllers to this scrollView and swipe between them.
I would like to add a view that I made in the storyboard into this UIScrollView. Is it possible?
I tried something along the lines of:
MYViewController *viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];
I've set the the scrollView size to be bigger than the screen and when the main view loads, I can see there is a scroll view (the scroll bars show) but my viewController is not inside it.
Anyone have any ideas?
The code you posted is still not right, on a couple of levels.
First, you should not use alloc/init for view controllers. You either need to use initWithNibName:bundle: (to create a view controller from a nib file) or instantiateViewControllerWithIdentifier: to load a view controller from a storyboard.
Second, you should not add a view controller's view as a subview of another view controller unless you use the parent/child view controller support that was added in iOS 5 and greatly improved in iOS 6. If you do what you are doing then all sorts of things won't work correctly: Auto-rotation, low memory warnings, background notifications etc. The list of things that can go wrong is unbounded.
The easiest way to do this is to add a container view as a frame to hold your child view controller, and then control-drag from your container view onto the scene that you want to set up as a child. This causes IB to set up and "Embed" segue. Embed segues do all the housekeeping you need to host one view controller's content inside another, with no code needed.
You could create a container view inside your scroll view's content view, and then it would just work fine.
I found the problem:
I was not allocating and initialising my viewController. Ooops.
This is the correct code:
BaseViewController *viewOne = [[BaseViewController alloc]init];
viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];

Can't change the size of a TableView in Interface Builder

I'm trying to change the size of a tableView I have in interface builder and can't seem to. When I first started the app I could drag it around and stretch the sides but all of a sudden I can't adjust it. I tried to delete my tableView and add a new one but the same thing happened. Thanks in advance. Here's what I see when I try to change the size:
if your using a UIViewController you can drag and drop a tableView and can place in a custom position you want. but if your using a UITableViewController you cant move the tableView to your custom position. if you want to do it in UITableViewController you can do like below
if you want your tableView content should show from a point, that you want you can do this way,
[self.tableView setContentInset:UIEdgeInsetsMake(100,0,0,0)];
else if you want set your tableView to a frame in UITableViewController you do this way,
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.tableView.frame = CGRectMake(0,100,320,300);
}
hope this will help you.
What kind of view controller are you using to manage your table view? Since you show "prototype cells", I'm guessing it's a UITableViewController. Those are built to fill the entire screen with a single table view (which has always annoyed me.)
Starting with iOS 6, though, you can create a "container" view in another view controler, and then drag an embed segue from your container view onto the table view controller. That does all the housekeeping to make the table view controller a child view controller of the other one, and then you can make it whatever size you want.
If you don't want to use a UITableViewController as a child of another view controller, you can use a regular view controller and wire up the data source and delegate methods yourself. However, things like static table views and prototype cells don't work then.
Hopefully this helps someone still coming across this problem. What I did was make sure the UIViewController had a UIView as its direct child, then dragged the UITableView as a child of the UIView, this allowed me to resize the UITableView.

Resources