Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating uiviewcontroller dynamically in Swift

Tags:

ios

swift

I want to create UIViewController dynamically without creating a class or using Mainstoryboard.I want it to happen programatically.Is it possible? I want to create UIViewControllers depending on the dynamic data.

like image 867
Nishat Jahan Avatar asked Jun 22 '16 08:06

Nishat Jahan


People also ask

What is UIViewController in Swift?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

How do I embed a view controller in a navigation controller programmatically Swift?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.


1 Answers

Yes, you can create a view dynamically, programmatically. I know you said you don't want to use a class, but you really have to. The key, though, is that you don't have to have this class hard code the UI, but rather build it dynamically.

So, you can subclass UIViewController, but pass it the data it needs to build the view dynamically and then implement the loadView method to do so. No NIB or storyboard scene is needed. Just create the view and build the subviews based upon your dynamic data. This is described in the legacy View Controller Programming Guide. (Please note that portions of that document no longer apply, notably al of the "unload" view discussion. But it does describe and illustrate the loadView process, which still works fine.)

The process for building a view for a view controller is as follows:

enter image description here

As you can see, one of the paths (the custom loadView method) bypasses the storyboard/NIB workflow. Note, if you go about this process, you are responsible for instantiating a UIView object and setting the view property of the view controller. Also, do not call super.loadView() in your implementation.

For example, if I wanted to just add a bunch of UILabel objects to the view, in Swift 3 you could do something like:

class ViewController: UIViewController {

    var strings: [String]!
    
    override func loadView() {
        // super.loadView()   // DO NOT CALL SUPER
        
        view = UIView()
        view.backgroundColor = .lightGray
        
        let stackView = UIStackView()
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical
        view.addSubview(stackView)
        
        NSLayoutConstraint.activate([
            stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
        
        for string in strings {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.text = string
            stackView.addArrangedSubview(label)
        }
    }
}

And you could present it like so:

@IBAction func didTapButton(_ sender: AnyObject) {
    let controller = ViewController()
    controller.strings = ["Hello world", "Foobar", "Baz"]
    show(controller, sender: sender)
}

(For Swift 2 renditions, see previous revision of this answer.)


Having said this, I'm not sure why you're concerned about not using a NIB or storyboard. Just use a blank one. And then, you can again programmatically add your controls to the scene dynamically, but do it in viewDidLoad. It achieves the exact same effect as the above, but avoids using the fairly antiquated loadView technique and from having to instantiate and load the view property yourself.

like image 90
Rob Avatar answered Sep 21 '22 15:09

Rob