How can I add a common view to multiple ViewControllers in iOS Swift? - ios

I'm kinda new to iOS development.
I want to have a custom View and some Labels inside it. And In some viewControllers of my app on a button click I want to add/show that View
at the bottom of that viewController.
As far as I'm manually adding the view in storyboard in all the viewControllers in which I want the view to display. But this is not efficient. How can I add this view in viewControllers programmatically on button click?

Make one BaseViewController class inherited with UIViewController
Now create method named as designFooter in BaseViewController
func designFooter() {
var viewFooter: UIView = UIView(frame: CGRectMake(0, self.view.bounds.size.height - 50, self.view.bounds.size.width, 50))
viewFooter.backgroundColor = UIColor.redColor()
self.view!.addSubview(viewFooter)
}
For Swift 4, 5.1:
func designFooter() {
let viewFooter: UIView = UIView(frame: CGRect(x:0, y:self.view.bounds.size.height - 50, width:self.view.bounds.size.width, height:50))
viewFooter.backgroundColor = UIColor.red
self.view.addSubview(viewFooter)
}
Now inherit this BaseViewController to you ViewController where you want to add footer, and on button click just call self.designFooter()

If this subview you want to add has some dynamic content or has much of its own logic, you might want to employ view controller containment, specifically not only add a subview, but add a controller associated with that subview, too. So, you can have a scene in your storyboard for this subview that will appear on the bottom, and associate it with its own view controller. Then, when you want to add it, you'd do something like:
let child = storyboard!.instantiateViewController(withIdentifier: "storyboardid")
addChild(child)
// set the `frame` or `constraints` such that it is in the correct place, perhaps animating it into place
view.addSubview(child.view)
child.didMove(toParent: self)
And when you want to remove it:
child.willMove(toParent: nil)
child.view.removeFromSuperview()
removeChild(child)
Personally, if this can really appear and disappear from any scene in my app I actually embed the whole app in a container view controller. Then this popping of the child in and out only has to be done once, on this master container view controller.
For example, consider this storyboard:
That is an embedded "container view" (the storyboard equivalent of view controller containment, discussed above). And I can then have a label animate in an out (by animating layoutIfNeeded after changing the height constraint of some view with a label). Then, this bottom view can animate in and out regardless of which view controller's view is currently visible:

Just create a UIView and call addSubview:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//change this frame for your purposes
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10,10,50,10)];
[l setText: #"My label"];
[view addSubview: l];
[self.view addSubview: view];
As far as adding this to multiple view controllers... I suppose if you had a lot of different view controllers with this same UIView you could create a subclass of UIViewController called CustomViewController. In that class add the above code to viewDidLoad. Then, subclass CustomViewController in all the view controllers with this particular view, and they will automatically add it for you.
Edit:
If you want to design the view in interface builder, make a custom subclass of UIView. Let's call this CustomView. Design it in a nib and add any code you want. Then, whenever you want to create that view, simply call CustomView *cv = [[CustomView alloc] initWithFrame:...] and then do [self.view addSubview:cv];

Related

How to SubView a ViewController into a SubView of another ViewController

I have two ViewController.
One ViewControllers contain a UITableView. And another contains a UIButton.
I have created a SubView Programmatically.Now i want to SubView the ViewController which contains the UITableView in other when i Press UIButton.
I searched all over the net but cannot find any stable solution.
Currently i am trying this:
bodyView =[[UIView alloc]initWithFrame:CGRectMake(0,120,containerView.frame.size.width,120)];
bodyView.backgroundColor = [UIColor redColor];
CustomTableVC *tableVC = [[CustomTableVC alloc]init];
[tableVC willMoveToParentViewController:self];
[bodyView addSubview:tableVC.view];
[self addChildViewController:tableVC];
[tableVC didMoveToParentViewController:self];
[containerView addSubview:bodyView];
You cannot.
You can only use the view property of your UIViewController to assign into UIView associated in your second UIViewController which is not recommended because UIViewController as per MVC pattern holds lot controller stuff which includes populating the view and resolving the inputs/touch, which is an overhead in your (using multiple of viewcontrollers without needed) case.
You need to use one UIViewController. Add UITableView only in it, and UIButton only in it. You only use one controller and multiple views.
The other approach, if you do not want to change your code, may also use ContainerView. But in that case you need to create separate ViewControllers for UIButton and UITableView. And if you want to fetch data inbetween the ViewControllers, that will be a huge pain and also a bad software design with so much coupling and less encapsulation.
i have tried this one and it's working for me.
#IBAction func moveToOther() {
var otherController = OtherViewController()
var bodyView = UIView(frame: CGRectMake(0,120, self.view.frame.size.width, 120))
bodyView.backgroundColor = UIColor.redColor();
bodyView.layer.borderWidth = 1.0
let tblCntrl = UITableViewController()
bodyView.addSubview(tblCntrl.tableView)
bodyView.clipsToBounds = true
otherController.addChildViewController(tblCntrl)
tblCntrl.didMoveToParentViewController(otherController)
otherController.view.addSubview(bodyView)
self.navigationController?.pushViewController(otherController, animated: true)
}
You should use only one view controller that contains both the table view & UIButton.
By default hide the table view.
Just hide the button and show the table view when the button is clicked.

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

uitableview inside multiple view controllers

I have to 'repeat' a uitableview into 2 or more view controllers. Like using a uitableviewcontroller with its methods inside view controllers using the methods/delegates/datasources from the uitablewviewcontroller.
Example: I've a uitableviewcontroller wich displays a twitter feed with its own style, so I need this table in more than 1 view controller and maybe just changing the twitteruser in each view controller (just an example).
TRY: What I've done is to create (with storyboard) a uiviewcontroller and a uitablewviewcontroller, both with their own methods. And on uiviewcontroller viewdidload, try to add the uitablewviewcontroller.tableview as a subclass. and this works! But the result is an empty table. I tried to set the delegates/datasources but it ddnt work..
-(void)viewDidLoad
{
[super viewDidLoad];
//Add Table as subview
thetable *t = [[thetable alloc] init];
[self.view addSubview:t.tableView];
t.tableView.frame = CGRectMake(0, 100, 320, 2000);
t.tableView.backgroundColor = [UIColor blueColor];
}
This code works, but just displays an empty uitable inside the view controller..
*thetable is a uitableviewcontroller object (.h, .m, and storyboard view)
*Just set the backgroundcolor to check if there's anything on screen
*using ios6
Thanks!
Make sure you are implementing the table view delegate and datasource methods in thetable.Also try [t.tableView reloadData] after adding the tableView to the view controller's view.

Manage uiview and uiviewcontroller

I created an uiview , that contained , many textfields , and I have an uiviewcontroller with xib file responsible for making signature .Is it possible to add this viewcontroller to my uiview in order to have this componant of drawing signature in the footer of my uiview ??
- (void)init { //parent view
...
FooterViewController *fvc = [[FooterViewController alloc] initWithNibName:#"myNibName" bundle:nil];
[self addSubview: fvc.view];
fvc.view.origin = CGPointMake(0, self.frame.size.height - fvc.view.frame.size.height);
}
this is how you can create your viewController from xib file and add its view to the main view. assuming this is what you are trying to do.
If you are using storyboards, the easiest way to go is to drag a Container View object from the Object library (where buttons, labels, etc. are) to your view controller's view. This will create a child view controller that you can handle separately and that will be already resized to mirror the size of the container view controller.

adding subviews in uiviewcontroller make it goes will

I have a custom UIView that I need to add as a subview in a UIViewController.
But if I use [self.view addSubview:newView]; the app goes in a infinity loop and doesn't start. But if I use self.view = newView then it works. But I need it as a subview.
The UIView contains a grid layout of custom button.
I guess you do that in the loadView method. You must not call the view method of the view controller in loadView because that will itself call loadView! However calling setView: (e.g. self.view = newView) is okay.
My suggestion is to add the subview in viewDidLoad.

Resources