How can we load user interface on in iPhone application without using nib file? I want to load user interface of application using .m file .Is it possible? how can we do that.
-
you can create `uiview` programatically... – NULL Jul 02 '13 at 04:47
-
possible duplicate of [iOS - Support iPad & iPhone without using nib](http://stackoverflow.com/questions/15401659/ios-support-ipad-iphone-without-using-nib) – rmaddy Jul 02 '13 at 04:48
4 Answers
If you want to do it programmatically, the loadView
method is intended for creating views when not using NIBs or storyboards. Just write a loadView
in your view controller and create your views there. Or have the loadView
create an instance of your UIView
subclass, and put the bulk of the code for your custom view in that UIView
subclass. That's an alternative to using Interface Builder to build a NIB or storyboard.
If you do it in loadView
, you just make sure to set self.view
to be the view you created, and then the rest of your view controller code can reference view
property like it would otherwise.
See the Creating a View Programmatically section of the legacy View Controller Programming Guide. It outlines the basic process and provides a code sample. Admittedly, some of that legacy document is not entirely relevant (e.g. notably, views aren't "unloaded" under memory pressure and thus any references to viewDidUnload
are no longer relevant), but it gives some context to programmatically created view hierarchies.
The A View Controller Instantiates Its View Hierarchy When Its View is Accessed [broken link] section of that guide used to outline the basic flow:
By way of example, here is a Swift 3 example loadView
for a view controller that programmatically creates its view hierarchy:
class ViewController: UIViewController {
weak var containerView: UIView!
weak var button: UIButton!
// Note, when programmatically creating views, you:
//
// - Implement the view hierarchy in `loadView` (not to be confused with `viewDidLoad`)
// - Must set `view` property
// - Must not call `super.loadView`
//
// This example creates a `view` for the view controller, and adds two subviews, a container and a button.
// Note, the "container" isn't really needed, but I just did it so I could visually demonstrate that the
// `frame` of the view was set properly. That's why I set it to be a different color of the main `view`
// and inset it.
override func loadView() {
view = UIView()
view.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
let containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
self.containerView = containerView
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
button.setTitle("Back", for: .normal)
button.setTitleColor(.white, for: .normal)
containerView.addSubview(button)
self.button = button
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 10),
containerView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: -10),
containerView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10),
containerView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10),
button.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
button.centerYAnchor.constraint(equalTo: containerView.centerYAnchor)
])
}
func didTapButton(_ sender: Any) {
// do something
}
}

- 415,655
- 72
- 787
- 1,044
-
Also note that you should *not* call `[super loadView]` from your own `loadView` method. – rmaddy Jul 02 '13 at 05:07
-
Agreed. The [Creating a View Programmatically](http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW36) in the _View Controller Programming Guide_ provides an example `loadView` method (and reiterates rmaddy's point). – Rob Jul 02 '13 at 05:09
Sample *sample = [[Sample alloc]init];
sample.view.backgroundColor = [UIColor whiteColor];
[sample.view addSubView:aLabel];
[self.navigationController pushViewController:sample animated:YES]; //or
[self presentViewController:sample animated:YES completion:nil];
It's just an example. Here Sample is UIViewController class without a Nib file. You can handle it how you handle UIViewController class with a Nib file.

- 2,020
- 3
- 15
- 20
Add view controller without xib. Then you can programatically add views. Eg.
In viewDidLoad method :
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0, 10, 480, 320); //giveframe for full screen
[self.view addSubView:view];
similarly for buttons, labels etc. Google it.

- 1,848
- 1
- 21
- 23

- 1,910
- 2
- 25
- 44
Override loadView in your UIViewController subclass. Initialize a view and any subviews you want. Make sure this method sets the view property of your view controller.

- 1,082
- 1
- 10
- 16