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.
Related
Due to design Constraints I have to add an UiViewController to UITableViewController through
[controller addChildViewController:self];//self = uiviewcontroller controller = Instance of UITableViewController
[controller.view addSubview:self.view];
It adds it with a bit lag, but I get a warning in my console
"setting the first responder view of the table but we don't know its type (cell/header/footer)"
I guess it is because controller.view is tableview and not normal uiview, it throws this error both in iOS6 and iOS7
How can I add a ChildViewController to UITableViewController?
Short answer: Don't. UITableViewControllers can't really contain anything else but a table view. As rob says in his answer, it's better to have a parent view controller that has a container view with your table view controller in it, plus anything else you need, and perhaps other child view controllers if you need to.
Your quickest fix for the layout you're looking for is to have a parent view controller that includes both your table view controller and the child view controller with the UITextField.
Or just a view controller with a UITableView (with it's data source and delegate set to your view controller) and your child view controller added.
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.
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];
I'm struggling with this problem, so I need your help. Basically I've programmed a complex table view controller (with NSFetchedResults protocol etc) and already used it in my app and it worked great. Since I want now exactly this table view inside another view (so I can add a small subview at the bottom of the screen), I'm really struggling how to do this! I know by know how to embed a simple table view inside another view and link it to it's view controller.
But how can I do this in my case with as little effort as possible? I mean can I somehow use this table view controller I already have even though the superview must have its own view controller?! And how would I do that? Sorry I'm still a beginner :)
Since you already have a TableViewController. To add it as an subview to another ViewController's (self) view, do this:
TVC = <your tableViewController instance>;
[self addChildViewController:TVC];
TVC.view.frame = <your desired frame>;
[self.view addSubview:TVC.view];
adding the TVC as childViewController will forward UI methods of 'self' like willAppear and all to TVC.
This is on iOS5 w/ a storyboard.
I'd like to have a UINavigationController on the UITableView below but when I try the "embed in" option, it adds it to the Red UIView, not on the table itself. For lots of reasons this is not optimal. Is what I want to do not possible: to have a table subview with its own nav controller?
Oh - while I am here - what is the deal with UINavigationControllers not being able to be resized in a storyboard? I can only set "form" "page" or "full" - when I set it to "freeform" I am not able to enter any values to resize it
For lots of reasons this is not optimal
Actually, for lots of reasons what you are trying to do makes no sense. A UINavigationController has embedded within it (in Storyboard terms) an instance of a UIViewController. In other words, the nav controller's root view controller must be a view controller. Since UITableView is a subclass of UIView, you can't embed it inside a UINavigationController. And besides, you would never want to. A UINavigationController manages a hiearchy of view controllers. What are you trying to achieve that you think you need to put a UITableView inside of a UINavigationController? What you are probably trying to achieve is to place the view controller that the table view sits on inside a UINavigationController, in which case the result you're seeing in IB is the correct result.
UINavigationController is a view controller, not a view, so you can't embed it inside a view.
You should be able to get what you want with a little code: you can't embed it inside Xcode, but you can set up the UINavigationController and the red view separately, then write a few lines like this:
navigationController.view.frame = CGRectMake(20, 20, 280, 300);
[redView addSubview:navigationController.view];