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:
Related
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
Pls, would you advise?
I am testing some old pod and I am getting "UIAlertView is deprecated and unavailable for UIScene based applications, please use UIAlertController"
I googled somewhere to remove scene delegate, comment out UISceneSession, etc..
I did all the steps and when I start the app the contentview is always black :(
Pls, how to instantiate the Content view from appdelegate?
Try this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
let timeline = YourViewController()
let navigation = UINavigationController(rootViewController: timeline)
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
window!.rootViewController = navigation
window!.makeKeyAndVisible()
return true
}
The SceneDelegate.swift doesn't have to be commented, there are some steps you have to be aware of.
Set a window instance to your AppDelegate window property, as #pkc456 recommended.
window = UIWindow(frame: UIScreen.main.bounds)
Remove the Scene configuration from the AppDelegate (You have already done it by commenting it as showed on your screenshot).
Delete the Scene configuration key from Info.plist file.
Info.plist > Application Scene Manifest > Scene Configuration
Remember to use the View Hierarchy debugger, sometimes you are seeing the correct view controller but it doesn't have a backgroundColor.
Hope I Helped!
I want to load initial view controller with xibs not with storyboard. As I am migrating objective c project to swift 4. Can any one have any idea to resolve this issue ?
To load xib as the initial view controller instead of a storyboard, you can achieve this as implied in the following sample code,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
loadInitialVC()
return true
}
func loadInitialVC() {
let testVC = TestNibViewController(nibName: "TestNibViewController", bundle: nil) as? TestNibViewController
window?.rootViewController = testVC
}
You might face issue when using the xib which I faced while trying, so if you found any issue like "Loaded nib but the view outlet was not set" do as in the image attached.
Try editing your Info.plist: delete the Main storyboard file base name key and add a Main nib file base name key with the base name (without .xib) of your initial XIB.
I found the solution of my question , the correct way to load a xib as initialViewController in swift 4
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let testVC = MyViewController(nibName: "MyViewController", bundle: nil) as? MyViewController
let navigationBar = UINavigationController.init(rootViewController: testVC!)
window?.rootViewController = navigationBar
window?.makeKeyAndVisible()
return true
}
Hope this solution will help of others too.
I took the one month iOS course and found that the content was a bit outdated because of the changes in Xcode. In lesson 4, the teacher taught us to change the background color in appDelegate.swift by first deleting the storyboard and viewController as well as eliminating the "main" tab in Info.plist. And then he typed in codes in appDelegate.swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.magentaColor()
self.window?.makeKeyAndVisible()
return true
}
But the syntax is little bit different in Xcode8.2.1, so i modified it to the code as below. But it doesn't work either.
Then i found that there were updated codes on Github for this course.
Unfortunately, it did't work either( but I know it's because it did not define "StaffPicksViewController").
So what should i do to change the background color without storyboard and viewController?
I received a great answer from G+:
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = UIViewController()
self.window?.backgroundColor = UIColor.magenta
self.window?.makeKeyAndVisible()
I just forgot to init a rootViewController...
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
}