Search This Blog

Friday, November 10, 2017

iOS Swift. How to create app without storyboard

There is a question: Is it possible to create iOS application . without using Storyboard?. Answer is Yes, it is possible. For implementing this we must create our custom UIWindow and set root controller for it. In code below I show how to do this.

AppDelegate class and didFinishLaunchingWithOptions method

In AppDelegate class method didFinishLaunchingWithOptions we create UIWindow and set root controller for it.
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.makeKeyAndVisible()
        
        let viewController = ViewController()
        let navigationController = UINavigationController(rootViewController: viewController)
        self.window?.rootViewController = navigationController
        
        return true
    }
}

ViewController

Our ViewController. We set title and add button to center of it using constraints.
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "No Storyboard"
        self.view.backgroundColor = UIColor.white
        
        let button =  UIButton(type: .system)
        button.setTitle("Success", for: .normal)
        self.view.addSubview(button)
        
        button.translatesAutoresizingMaskIntoConstraints = false
        button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        button.widthAnchor.constraint(equalToConstant: 200).isActive = true
        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
    }
}

Result


5 comments:

  1. One of the key features said to be coming to
    the HomePod is the ability to make or receive phone calls
    Read More At iOS Online Training Bangalore

    ReplyDelete
  2. Thank you very much for drawing my notice to this. Your blog is jam-packed with useful facts. Until I read that, I would have no idea. I'll come back for more fantastic material. Best wishes, and good luck.
    -Custom Web Design

    ReplyDelete
  3. This is an excellent article! Thank you a lot. I'm in desperate need of these suggestions. The in-depth explanation is quite beneficial. It was incredibly encouraging to see how you write. Best Custom Websites

    ReplyDelete
  4. This is a great article! Thank you very much. I really need these suggestions. An in-depth explanation is of great benefit. Incredibly inspiring to see how you write. Custom Website

    ReplyDelete
  5. Thanks for sharing such a great information.It really helpful to me.I always search to read the quality content and finally i found this in you post. keep it up! On Demand Developers

    ReplyDelete