How to set the rootViewController with Swift, iOS 7
You can do something like this.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
return true
}
set root view controller in main storyboard
change storyboard--> viewcontroller---> attribute inspector---> change presentation from Automatic to Full Screen
Changing RootViewController type in Swift
You should subclass UIViewController
and make your own version to check it works, but what you're doing originally is fine
let myViewController = SomeViewController()
let navigationController = UINavigationController(rootViewController: myViewController)
window?.rootViewController = navigationController
Then in SomeViewController
viewDidLoad
set view.backgroundColor = .red
If you want to remove the navigation bar you can set it to hidden
navigationController.navigationBarHidden = true
alternatively...
let myViewController = SomeViewController()
window?.rootViewController = myViewController
Will also work... though you should be looking to keep the navigation controller in general.. it usually makes presenting view controllers a lot easier in the future...
The reason your simulator goes black is because it worked... you're showing an empty UIViewController
... You must make your own UIViewController
subclass and add stuff to it.
Your View Controller subclass should look like this
//
// SomeViewController.swift
// SomeProject
//
// Created by Magoo on 17/10/2016.
// Copyright © 2016 Magoo. All rights reserved.
//
import UIKit
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
let label = UILabel(frame:view.bounds)
label.textColor = UIColor.whiteColor()
label.text = "Hello world"
view.addSubview(label)
}
}
The result should be a red screen with 'Hello world' written in the centre.
Set rootViewController programmatically not working, picking initial view controller from storyboard only in Xcode 11
Got the answer by doing some more google, i mean stackoverflow.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
window = UIWindow(windowScene: windowScene)
window?.rootViewController = vc
window?.makeKeyAndVisible()
}
SceneDelegate is used for this purpose since iOS 13 not AppDelegate. Here is the link SceneDelegate example
Change root ViewController programmatically
Set this in appDelegate's didFinishLaunchingWithOptions
method according to current app settings , also you should use window
not keyWindow
if(userExists)
{
let vc = storyboard.instantiateViewController(withIdentifier: "tabBarVC")
UIApplication.shared.window.first?.rootViewController = vc
}
else
{
let vc = storyboard.instantiateViewController(withIdentifier: "loginVC")
UIApplication.shared.window.first?.rootViewController = vc
}
Don't use present as this will automatically change the root
Also another way to access windows (used when AppDelegate has a value)
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
appDelegate?.window?.rootViewController = homeController
Related Topics
How to Make a Mkannotationview Touch Sensitive
Ignore Manual Entries from Apple Health App as Data Source
Xcode 5/iOS 7 - Localization Not Working in Simulator
Swiftui How to Align a View That's Larger Than Screen Width
Not Getting the Email Using Google Authentication in Firebase
Get Each Line of Text in a Uilabel
Alamofire 5: Value of Type 'Result<Data, Aferror>' Has No Member 'Value'
Change Tabview Color Based on Which Tab Is Selected -Swiftui
Uisearchbar's Cancel and Clear Buttons Not Working in iOS 7
Facebook App Invites Notification Not Working in iOS
iOS Client Certificates and Mobile Device Management
Navigationcontroller.Navigationitem VS Navigationitem
Xcode Duplicate Symbols for Architecture Error After Updating Cocoa Pods
Hit Detection When Drawing Lines in iOS
How to Control My Headset for My Music Player