1

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?

1
  • are you using UIViewController or UITableViewController ? Commented Jul 2, 2016 at 6:06

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. It's working. was expecting like this. And one more thing, after the button click how can I make the view permanent for all the viewcontrollers means now if I go to another viewcontroller I won't have to click the button to show the view, it will be already there.
Objective-C won't be problem. If I face difficulty I'll ask.
store some flag on button click on singleton variable or in NSUserDefault what best for you. and based on that show your View.
This is incompatible with storyboards
4

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

3 Comments

Ok. I'll give a try
How to add a common collectionview for multiple viewcontrollers?
I’d probably be inclined to do put the logic in a UICollectionView subclass then use view controller containment to embed that scene in which ever other scenes you need that functionality.
0

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

5 Comments

In this way I have to implement this code in all the viewControllers, right? Can i create this view in one nib file or like so , and just add this to the viewcontrolllers?
Yes, you can use interface builder. See edit @MustahsinulMoula
The OP is asking about the swift syntax. Since he is new to iOS development objective c code will confuse him more. Please consider adding swift code to support your answer.
I mean, they're very very similar. I don't do Swift so I don't know it off the top of my head, but he knows what a nib is and I'm sure he recognizes initWithFrame... @JeetendraChoudhary
I'm okay with objective-c. No problem. Any idea will be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.