How to Programatically Create a UIviewcontroller in Swift

programmatically present a new view controller in swift/programmatically give a class an Identifier

If you aren't using the storyboard you don't need an identifier. Just instantiate and present it.

let loadVC = SelectionScreen()
self.present(loadVC, animated: true, completion: nil)

How do I create a UIViewController programmatically?

UIViewController *controller = [[UIViewController alloc] init];
controller.view = whateverViewYouHave;

Do you have your own view controller that you coded? In that case you probably don't need to set the view property as it has been set in IB if that is what you used. When you have your controller you can push it onto the navigationController or view it modally etc.

How to create a UINavigationControll in UIViewController programmatically Swift

Maybe have a look at "Showing and Hiding View Controllers" where some navigation concepts are explained: https://developer.apple.com/documentation/uikit/view_controllers/showing_and_hiding_view_controllers

When using storyboards you should be able to use a segue to navigate from one view controller to another.

Instantiate UIViewController programmatically without nib

According to the docs:

If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/view

If you do not provide a nib file when instantiating the view controller, UIViewController calls it's loadView() method which creates a plain UIView object and assigns it to its view property.

The reason why you are seeing a transparent view is because the UIView object has no background color. To verify this, you can set the background color of the view controller's view property right before you push it on your navigation stack.

let viewController = TestViewController()
viewController.view.backgroundColor = .blueColor()
navigationController?.pushViewController(viewController, animated: true)


Related Topics



Leave a reply



Submit