I have UIViewController that i created programmatically. This is the code. Even after setting my SignUpViewController background to white. I am getting black screen on launch
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
startWindowLoad()
return true
}
//extension with the starter function
extension AppDelegate {
func startWindowLoad () {
let startView = SignUpViewController()
let navView = UINavigationController()
navView.pushViewController(startView, animated: true )
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = startView
}
}
In a modern app, the app delegate window is not used. You need to use the scene delegate window. Move all the code into the scene delegate.
This is probably because your app is starting with LaunchScreen
Goto Info.plist and
change Launch screen interface file base name to Main
Related
Short question:
How can I launch and make a UITabBarController be the rootViewController of my app after starting with a Storyboard?
Long question:
I'm not a swift expert, but I managed to create a complete app using XIBs from the beginning. Now I need my app to start with a Storyboard as a new requirement to post updates to the appstore from 01/07/2020, but I never used it to build my views. It was easy to modify my app to have my Storyboard as an entry point, but the problem is that my initial view today is a TabController, and I don't know how to navigate from my initial Storyboard to my TabController.
My AppDelegate today works something like this:
var window: UIWindow?
func application(_application: UIApplication, didFinishLauchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// initiate UINavigationControllers and UITabBarController here...
tabController.viewController = [nav1, nav2, nav3, nav4]
tabController.modalPresentationStyle = .fullScreen
self.window!.rootViewController = tabController
self.window!.makeKeyAndVisible()
}
All my attempts ended with a white screen after showing my Storyboard without showing my TabBar.
One of these attempts was this:
override func loadView() {
super.loadView()
// initiate tabController the same way I did in the AppDelegate
UIApplication.shared.keyWindow?.rootViewController = tabController
}
Check the value "Is initial View Controller" for it.
This question already has an answer here:
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
(1 answer)
Closed 3 years ago.
I am trying to programmatically set the initial View controller but i keep getting this Error. Any solutions?
2019-11-07 11:47:43.975990+0000 RestaurantApp[16319:147412] [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Here is the code that i have Written.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let window = UIWindow()
let locationService = LocationService()
let storyboard = UIStoryboard(name: "Main", bundle: nil) //refernce to our storyboard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//setiing the root view control on our window
switch locationService.status {
case .notDetermined, .denied, .restricted:
let LocationViewController =
storyboard.instantiateViewController (withIdentifier: "LocationViewController") as? LocationViewController
LocationViewController?.locationService = locationService
window.rootViewController = LocationViewController
default:
assertionFailure()
}
window.makeKeyAndVisible()
return true
}
}
Here is an Image of my storyboard
iOS 13 has moved the windows setup from AppDelegate to SceneDelegate to support the use of (possibly multiple) scenes rather than a single window. You now have to do the setup like this:
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()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let homeView = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.window?.rootViewController = homeView
return true
}
this works for me
This error happens due to a simple mistake in your storyboard, and it’s easy to fix. When your app starts, iOS needs to know precisely which view controller needs to be shown first – known as your default view controller.
If you accidentally deleted that view controller, or otherwise made it not the default, then you’ll see the error “Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?” when your app launches, along with a plain black screen.
To fix the problem, open your Main.storyboard file and find whichever view controller you want to be shown when your app first runs. When it’s selected, go to the attributes inspector and check the box marked “Is Initial View Controller”. You should see a right-facing arrow appear to the left of that view controller, showing that it’s your storyboard’s entry point.
Go back to storyboard and checkmark this to make the viewController you want your app to start off to,
I was refactoring an existing project to move away from using .storyboards and removed the initial main interface in .plist, but for some reason the app is creating two instances of UIWindow.
I have no idea on why this is happening, and the result of this is when I do create my actual UIWindow and use makeKeyAndVisible() for a second I get a black screen until the actual rootViewController becomes visible, this happens because in that split of a seconds it shows the first UIWindow which color is nil. If someone has any idea on why this is happening I would appreciate a bunch ;)
EDIT 1:
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
appCoordinator.start()
return true
}
AppCoordinator.swift
func start() {
let mainVC = UIStoryboard(storyboard: .main).instantiateInitialViewController()
window.backgroundColor = .white
window.rootViewController = mainVC
window.makeKeyAndVisible()
}
Check that "Main Interface" is empty:
I am using Swift 3 and I researched various methods to set the backgroundColor color of a UITabBar. The most simple method was to change the property in the didFinishLaunchingWithOptions of the AppDelegate. When I tried to do this, the UITabBar launches with the standard color, which is not my intended result.
However, when I go to the next tab, the UITabBar color gets changed to my intended color.
Following this, I tried to subclass my UITabBarController and I tried to set the background colors in both the viewDidLoad and in the viewDidLayoutSubViews. All three of these attempts exhibited the exact same behavior - which was the UITabBar launched with the standard color, and only changed colors when I tabbed to the next tab.
Here is my code:
App Delegate
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
UITabBar.appearance().backgroundColor = UIColor.red
return true
}
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.backgroundColor = UIColor.red
}
viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
self.tabBar.backgroundColor = UIColor.red
}
My questions are the following:
1) Can I eliminate this as being a constraint issue? I didn't modify or set any constraints for the UITabBar
2) I have a launch screen where the user selects acknowledges something and then they are segued to the UITabBarController. Could this be part of the issue?
The basic way to change to a tab bar's color is not to set its backgroundColor as you are doing, but rather to set its barTintColor. (For more sophisticated control, use a colored backgroundImage.)
Putting this in my app delegate gets me a tab bar that's tinted red.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarVC = UITabBarController()
tabBarVC.viewControllers = [FirstViewController(), SecondViewController()]
tabBarVC.tabBar.backgroundColor = UIColor.red
window?.rootViewController = tabBarVC
window?.makeKeyAndVisible()
return true
}
I am very new to iOS development. In my app I have a tab bar and in one of the tabs I have a UISplitViewController. My issue is that when I go to the tab it shows the Detail view first. Then I have to click the back button to get the the master view. I have found one other person having this issue on stackoverflow, but the solution was in Objective-c and I am using the storyboard (not sure how to attach a class to it) and swift, so that did not help.
It also does not work when using an ipad in portrait mode. When I shift to landscape it works fine, but just shows a black screen (no back button) in portrait mode. Any help would be appreciated. Thanks.
I am not sure what other info you need or what you want me to show, so let me know if I left something out.
adjusted appdelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let tabBarController = self.window!.rootViewController as! UITabBarController
let splitViewController = tabBarController.viewControllers![3] as! UISplitViewController
///////////////////Always visible property
splitViewController.preferredDisplayMode = .AllVisible
///////////////////
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true
}
Update: ended up fixing following this answer Open UISplitViewController to Master View rather than Detail
I have created a sample SplitViewController in the project and set the property in the appdelegate. This works for me https://github.com/harsh62/stackoverflow_TestMasterDetailApp
splitViewController.preferredDisplayMode = .AllVisible
The full function is as follows:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
///////////////////Always visible property
splitViewController.preferredDisplayMode = .AllVisible
///////////////////
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true
}
References:
UISplitViewController - set always visible master controller when