App starts with black screen in iPhone - ios

I changed "Launch Screen File" to my "Main.Storyboard" in settings. Before doing this app was working fine, and opened directly without any problem. But after adding this there was a black screen before the launch of storyboard. This is very bad user experience. Help me to solve this. AppDelegate and viewController has nothing big. I have a LaunchScreen.xib in project and also LaunchImage in image assets. I am using Storyboard in project. I don't want a launchScreen in my app, I just wanna directly go to my storyboard.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = utilitiesObjet.getViewController("MainVC", mainStoryBoardName: "Main")
self.window?.makeKeyAndVisible()
Fabric.with([Crashlytics.self()])
return true
}
I solved it by adding a LaunchScreen.StoryBoard, Instead of using a LaunchScreen.xib. Thank you All :)

use this code in appdelegate this code for objective c
.....did finish launching with option method...
self.winow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"<Your Storyboard name>" bundle:nil];
LoginViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"<your view controller storyboard identifer name>"];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:ivc];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
For Swift....
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Override point for customization after application launch.
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
return true
}

Launch Screen different Main.StoryBoard. In Launch Screen File, you must type: "LaunchScreen" and choose file Lscreen.storyboard (xcode7), or .xib if Xcode6

Related

iOS - Setting 'rootViewController' results in black screen on device only

With a very basic single view application, I've deleted the main storyboard file and removed any references to it. As such I'm setting the window rootViewController programmatically. However, while this displays the single view (containing a label) correctly in simulator, it displays a black screen when running it on device. Here is the only code for the app.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = DummyViewController()
window?.makeKeyAndVisible()
return true
}
I've removed the entry for Main storyboard from the info.plist file, as well as the 'Main Interface' entry in the General settings.
I'm using Swift 3 and targeting an iOS 8 device. I'm using XCode 8.3.1.
There is no output in the console, and there are no exceptions. The viewDidLoad function is even triggering on breakpoint, so the codepath seems to be running correctly.
Any ideas?
Here's the bare bones code for DummyViewController upon request.
class DummyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
General settings showing no reference to Main Interface
Here is the image for the .xib linked to DummyViewController
** The solution to get around this case is to manually specify the .xib to load for the DummyViewController **
It looks like the ViewController is not set to display anything. Unless you are using a xib (in which case you would need to load the view controller in a different way, see below), there is nothing describing how the ViewController's view should render.
To test this out, you can add the line self.view.backgroundColor = UIColor.red to the ViewController's viewDidLoad() method, then run it again on the device- if the background color turns red, then hooray! The next step will be programmatically adding a UILabel.
Loading UIViewController From a XIB
let vc = MyViewController(nibName: "xibname", bundle: nil)
Alternatively, you can mask the loading by adding a custom init inside MyViewController:
class MyViewController: UIViewController {
required init() {
super.init(nibName: "xibname", bundle: nil)
}
}
(Thank you zonily-jame for the addition of hiding it in the class)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginView : BaseClassVC = mainStoryboardIpad.instantiateViewControllerWithIdentifier("BaseClassVC") as BaseClassVC
let navigationController = UINavigationController(rootViewController: loginView)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
change Your window root as and set color
let viewController:DummyViewController = DummyViewController()
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = viewController
And change your controller
override func viewDidLoad() {
self.view.backgroundColor = UIColor.white
}
You just need to initialize viewcontroller object and implement white background color to its instance.
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
//Add below 2 lines
let vc = DummyViewController()
vc.view.backgroundColor = .white
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}

How to set root view controller?

I want to make slide out menu. For this I have to create UINavigationViewController which will controlMenuViewController(Table View) and ProfileViewController(Content View)
I want to set UINavigationViewController like a rootViewController, for this I wrote this code in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let nav1 = UINavigationController()
let mainView = MainViewController(menuViewController: nil, contentViewController: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window?.rootViewController = nav1
self.window?.makeKeyAndVisible()
return true
}
MainViewController is my UINavigationViewController.
But here I have error
nil is not compatible with expected argument type 'UIViewController'
What I should do?
You pass nil here: MainViewController(menuViewController: nil, contentViewController: nil) instead of some view controllers. That's probably the reason it doesn't compile.
You embed your MainViewController instance, which is already UINavigationController, into another one UINavigationController. That looks wrong too.
Here is an example:
LoginViewController *loginController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;`
This error means you can't set menuViewController and contentViewController to nil, they are not optional

iOS 9 selecting tabbar index with quick actions, performActionForShortcutItem

I have 5 tabs and want to go to a specific tab when user select a certain Quick Action.
However, I've tried using notification center, referencing the master viewcontroller and referencing the tab in app delegate but none seems to work. The tabbar.selectedIndex method does get called but for some reasons the tab isn't changing when using quick action.
#available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
let revealVC = self.window!.rootViewController as! SWRevealViewController
let tabVC = revealVC.childViewControllers[1] as! UITabBarController
let navVC = tabVC.viewControllers![0] as! UINavigationController
let shopVC = navVC.viewControllers[0] as! BrowseVC
switch shortcutItem.type {
case "com.modesens.search" :
tabVC.selectedIndex = 0
//referencing method to go to tab in base view controller also doesn't work...
//shopVC.goToSelectTab (0)
completionHandler(true)
//notification center method gets called but tab actually doesn't change
//NSNotificationCenter.defaultCenter().postNotificationName("goToTab", object: nil, userInfo: ["tabIndex":0])
default:
print("no work")
}
completionHandler(false)
}
revealVC is parent, tabVC is child of revealVC, then navVC is child of tab.
Again, I've tried using notificationCenter and referencing the shopVC, then calling this method:
Recently, I have implemented home screen quick action with SWRevealVC library. So, I'm very glad to tell you how to solve this issue.
1) create SWRevealVC programmatically(not in the storyboard)
If you have 5 tabs and you want to move another tab with quick action,
you should change frontVC of SWRevealVC dynamically to response to quick action.
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *frontViewController = [storyboard instantiateViewControllerWithIdentifier:#"mainNaviVC"];
SideMenuViewController *rearViewController = [storyboard instantiateViewControllerWithIdentifier:#"sideMenuVC"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearViewController
frontViewController:frontViewController];
self.window.rootViewController = mainRevealController;
2) implement logic in the didFinishLaunchingWithOptions method
As apple documented mentioned, it is the best place for quick action logic in didFinishLaunchingWithOptions if you want to change first page.
In my case, performActionForShortcutItem is only called to deal with app's background.(you should return NO in didFinishLaunchingWithOptions to handle quick action in didFinishLaunchingWithOptions)
if ([shortcutItem.type isEqualToString:shortcutAroundStop]) {
handled = YES;
UINavigationController *frontViewController = [storyboard instantiateViewControllerWithIdentifier:#"naviStopAroundVC"];
SideMenuViewController *rearViewController = [storyboard instantiateViewControllerWithIdentifier:#"sideMenuVC"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearViewController
frontViewController:frontViewController];
self.window.rootViewController = mainRevealController;
[self.window makeKeyAndVisible];
}
And I give a sample project what I make(objective-c) for you. I hope it will solve your problem.
https://github.com/dakeshi/3D_Touch_HomeQuickAction

When set root view controller programmatically navigation and tab bar missing

App delegate code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myViewController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as UIViewController
if self.window != nil {
self.window!.rootViewController = myViewController
}
When I load app without this code everything is fine.
But if I run with this code view is loaded but navigation and tab bar are missing.
What am I missing here?
I am using storyboard.
This is code that work:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainRootController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as UIViewController
if self.window != nil {
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController
navigationController.viewControllers = [mainRootController]
self.window!.rootViewController = navigationController
}
return true
Before adding root view controller you must be allocate memory for navigation controller and then navigation controller take as a root view controller .
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:yourViewController];
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
self.window.rootViewController = navigationController;

RootView navigation in swift

I am an Objectie-C developer. for the root view navigation i used the below code in Objectie-c
FirstViewController *fvc=[[FirstViewController alloc]init];
UINavigationController *nv=[[UINavigationController alloc]initWithRootViewController:fvc];
self.window.rootViewController=nv;
for my new project I am making it in swift.I just want to make RootView navigation from AppDelegate.swift.
Check below code. first we create window. then alloc init viewcontroller. and then alloc navigation controller with rootcontroller as viewcontroller. and Window's root controller as navigation controller.
var window: UIWindow?
var navC : UINavigationController?
var vc:ViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// alloc init window
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// view controller
self.vc = ViewController(nibName: "ViewController", bundle: nil);
// create navigation controller with root = vc.
self.navC = UINavigationController(rootViewController: self.vc!);
self.navC?.navigationBar.hidden = true;
// window's root controller as navigation controller.
self.window?.rootViewController = self.navC
self.window?.makeKeyAndVisible()
return true
}
Maybe this will help you.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootVie: FirstViewController = FirstViewController() // this is allocation method in swift
if let window = self.window{
window.rootViewController = rootVie
}
return true
}
need referene use this link

Resources