Mustahsinul Moula
Mustahsinul Moula

Reputation: 103

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

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?

Upvotes: 1

Views: 8838

Answers (3)

sanjeet
sanjeet

Reputation: 1510

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()

Upvotes: 6

Rob
Rob

Reputation: 437372

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:

enter image description here

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:

enter image description here

Upvotes: 4

John Farkerson
John Farkerson

Reputation: 2632

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

Upvotes: 0

Related Questions