I presented a login screen as follows modally. Correct me if I am not right.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"login"];//LOOK AT NEXT LINE
[self presentViewController:ivc animated:YES completion:nil];
The login screen does appear but the animation is a slide up one. I prefer a fade in and fade our animation. How can I do this?
Just set the modalTransitionStyle property for the viewController. (Documentation)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"login"];
[ivc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:ivc animated:YES completion:nil];
In Swift:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "login")
viewController.modalTransitionStyle = .crossDissolve
present(viewController, animated: true, completion: nil)
Swift 3, 3.1, 4, 4.2 (as of January, 2021)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var loginViewController = storyboard.instantiateViewController(withIdentifier: "login")
loginViewController.modalTransitionStyle = .crossDissolve
self.present(loginViewController, animated: true, completion: nil)
You have to set prestentation and transition style:
self.activityAlertVC.modalPresentationStyle = .custom
self.activityAlertVC.modalTransitionStyle = .crossDissolve
Related
I am currently showing a viewController in my new storyboard below:
var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
self.presentViewController(vc, animated: true, completion: nil)
However, this presents the viewcontroller without its embedded navigation controller.
I tried changing "WelcomeID" to the navigation controller within the storyboard - however to no success.
I got this working in Objective -C, however don't know how to transform into swift:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = #"Hello";
[self presentViewController:navigationController1 animated:YES completion:nil];
How can you do this in swift?
You're definitely on the right track. Unfortunately when you reference a view controller by its storyboard ID it will ignore the fact it is embedded within anything. Same goes for segues when you segue to something embedded, the destination view controller will be the embedding controller, not the controller you're usually interested in. Anyhow, you should be able to fix the problem in a similar way you've done in Objective-C, so this is just an exercise in syntax porting.
Edit: Define storyboard name with string now
let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
OR you can give your embedding view controller an ID and instantiate that instead.
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeID") as SecondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
Class Name is : SecondCiewController
The answer given by #Chris is works good in older version of swift.
Update Swift 3 & Swift 4
let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeID") as! WelcomeViewController
vc.teststring = "hello"
let navigationController = UINavigationController(rootViewController: vc)
self.present(navigationController, animated: true, completion: nil)
Thanks!!!
Remember to setup the modalPresentationStyle property, in programmatically present the IB settings are ignored.
let sb = UIStoryboard(name: "retail_carrello", bundle: nil)
guard let carrelloVC = sb.instantiateViewController(withIdentifier: "mainCarrello") as? retail_carrello else { return }
let navController = UINavigationController(rootViewController: carrelloVC)
navController.modalPresentationStyle = .fullScreen //<--- remember to set up this if you need fullscreen
present(navController, animated: true, completion: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC
navigationController?.pushViewController(vc, animated: true)
I have created my project using Storyboards. The root ViewController lies inside a Storyboard, I have not written a single code in the appDelegate.
Now I want to show a tour of my app, so I want to change the root ViewController from Tab Bar to my TourVC and when the tour of the app is finished , I want to again switch back my root ViewController to Tab Bar.
So I looked up online and followed the following points
1) Remove Storyboards from app.plist file,
2) Uncheck option "isInitialViewController" from Storyboards which is checked in case of Tab Bar controller because its a root ViewController,
3) Add this code in appDelegate.m file.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ProductTourViewController *PT = [[ProductTourViewController alloc] initWithNibName:#"ProductTourViewController" bundle:nil];
self.window.rootViewController = PT;
[self.window makeKeyAndVisible];
return YES;
But my app crashes with this error log,
[ProductTourViewController selectedViewController]: unrecognized selector sent to instance 0x1766a9e0
And also I get a warning,
Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:.
Objective-C:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"tabBarcontroller"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
Swift :
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController
UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;
Swift 3:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Swift 5:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
Or simply like this:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
Both works fine!
Set storyboard ID for your class in your main storyboard.
UIStoryboard *MainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
UINavigationController *controller = (UINavigationController*)[MainStoryboard
instantiateViewControllerWithIdentifier: #"RootNavigationController"];
LoginViewController *login=[MainStoryboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[controller setViewControllers:[NSArray arrayWithObject:login] animated:YES];
self.window.rootViewController=controller;
In swift we can implement it is as following
let storyboard = UIStoryboard(name: "StartingPage", bundle: NSBundle.mainBundle())
let loginView: SignInVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInVC
UIApplication.sharedApplication().keyWindow?.rootViewController = loginView
I use simple this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"NameOfStoryBoard" bundle:nil];
UITabBarController *rootViewController = [sb instantiateViewControllerWithIdentifier:#"NameOfTabBarController"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
Just to add to Sunny Shah's answer, this is the Swift 3 version of it:
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Swift 3 code:
Use below in didFinishLaunchingWithOptions Appdelegate function.
Replace "HomeViewController" with ViewController you want to set as Root ViewController on app launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
Swift 5 + Xcode 11:
Make like this:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.windows.first?.rootViewController = viewController
UIApplication.shared.windows.first?.makeKeyAndVisible()
Or like this:
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
self.view.window?.rootViewController = viewController
self.view.window?.makeKeyAndVisible()
Both works fine!
Objective c
step 1: remove main story board from info.plist
step 2: add storyboard id to your view controller in your interface builder
step 3: add the following code to application did finish method in app delegate
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
//set main story board
if( condition){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName1" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
[self window].rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName2" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
[self window].rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}
This is an old article, but I will reply.
I do not recommend the following code.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarcontroller") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = viewController
Because you are creating two instances.
I recommend writing the following code in the appropriate ViewController.
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self
Edit: When solving this problem, I found that it is much easier to start with your UITabBarController, then perform login validation via your AppDelegate.m's didFinishLaunchingWithOptions: method.
Question:
This code is in the the application didFinishLaunchingWithOptions: method in my AppDelegate.m
if([result isEqualToString: #"log"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"TabBarControl"];
[(UINavigationController*)self.window.rootViewController pushViewController:ivc animated:NO];
NSLog(#"It's hitting log");
}
It simply takes an HTTP response for the user being logged in, and takes them to my TabBarController. The problem is that it's using a push rather than a modal transition to display the page. Since presentModalViewController method is deprecated or deleted in iOS7, how can I programmatically force a modal presentation?
EDIT:
)
Swift
This is how you would do it in Swift without referencing the navigation controller:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller, and id as needed.
See also how to dismiss a view controller programmatically.
In swift 4.2 you can do it like this. For those who want this answer in swift updated version.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)
In Swift 3/4
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyB.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController =
storyB.instantiateViewController(withIdentifier:
"SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
In the secondViewController use this code to go back.
self.dismiss(animated: true, completion: nil)
My problem is that I was looking for way to use both storyboard and xib. But I can't find proper way to load and show storyboard programmatically. Project was started developing with xib, and now it's very hard to nest all xib files in storyboard. So I was looking a way to do it in code, like with alloc, init, push for viewControllers. In my case I have only one controller in storyboard: UITableViewController, which has static cells with some content I want to show. If anyone knows proper way to work both with xib and storyboard without huge refactoring, I will appreciate for any help.
In your storyboard go to the Attributes inspector and set the view controller's Identifier. You can then present that view controller using the following code.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
Swift 3
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "viewController")
self.navigationController!.pushViewController(vc, animated: true)
Swift 2
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")
self.navigationController!.pushViewController(vc, animated: true)
Prerequisite
Assign a Storyboard ID to your view controller.
IB > Show the Identity inspector > Identity > Storyboard ID
Swift (legacy)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewController
self.navigationController!.pushViewController(vc!, animated: true)
Edit: Swift 2 suggested in a comment by Fred A.
if you want to use without any navigationController you have to use like following :
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = Storyboard.instantiateViewController(withIdentifier: "viewController")
present(vc , animated: true , completion: nil)
In attribute inspector give the identifier for that view controller and the below code works for me
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
Try this
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"Login"];
[[UIApplication sharedApplication].keyWindow setRootViewController:vc];
in swift
NavigationController and pushController you can replace for
present(vc, animated:true , completion: nil)
For swift 4 and 5, you can do this. Good practice is set name of Storyboard equal to StoryboardID.
enum StoryBoardName{
case second = "SecondViewController"
}
extension UIStoryboard{
class func load(_ storyboard: StoryBoardName) -> UIViewController{
return UIStoryboard(name: storyboard.rawValue, bundle: nil).instantiateViewController(withIdentifier: storyboard.rawValue)
}
}
and then you can load your Storyboard in your ViewController like this:
class MyViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
guard let vc = UIStoryboard.load(.second) as? SecondViewController else {return}
self.present(vc, animated: true, completion: nil)
}
}
When you create a new Storyboard just set the same name on StoryboardID and add Storyboard name in your enum "StoryBoardName"
You can always jump right to the root controller:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
The extension below will allow you to load a Storyboard and it's associated UIViewController. Example: If you have a UIViewController named ModalAlertViewController and a storyboard named "ModalAlert" e.g.
let vc: ModalAlertViewController = UIViewController.loadStoryboard("ModalAlert")
Will load both the Storyboard and UIViewController and vc will be of type ModalAlertViewController. Note Assumes that the storyboard's Storyboard ID has the same name as the storyboard and that the storyboard has been marked as Is Initial View Controller.
extension UIViewController {
/// Loads a `UIViewController` of type `T` with storyboard. Assumes that the storyboards Storyboard ID has the same name as the storyboard and that the storyboard has been marked as Is Initial View Controller.
/// - Parameter storyboardName: Name of the storyboard without .xib/nib suffix.
static func loadStoryboard<T: UIViewController>(_ storyboardName: String) -> T? {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: storyboardName) as? T {
vc.loadViewIfNeeded() // ensures vc.view is loaded before returning
return vc
}
return nil
}
}
I have multiple UIViewControllers in my iOS App's Storyboard. I want to open one of the UIViewControllers (in the storyboard) from a different UIViewController.
I've tried the code below, but it isn't working even though I used it before iOS 5 and it worked fine.
- (IBAction)addNotes:(id)sender {
NotesViewController *notesView = [[NotesViewController alloc] initWithNibName:#"NotesViewController" bundle:nil];
[self presentModalViewController:notesView animated:YES];
}
How can I perform this action with iOS 5 Storyboards?
Assuming you have storyboard, go to storyboard and give your VC an identifier (inspector), then do:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];
Assuming you have a xib file you want to do:
UIViewController *vc = [[UIViewController alloc] initWithNibName:#"NIBNAME" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
Without a xib file:
UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
Following will work on Swift 3.0 and above.
StoryBoard
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "Identifier")
self.navigationController?.pushViewController(mainViewController, animated: true)
.xib
let viewController = UIViewController(nibName: "NibName", bundle: nil)
self.navigationController?.pushViewController(viewController, animated: true)
Without .xib
let viewController = UIViewController()
self.navigationController?.pushViewController(viewController, animated: true)
Swift 5
let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ControllerIdentifier")
self.present(vc, animated: true, completion: nil)
Update with the Swift 3.1 version
if you haven't embed navigation controller then
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.present(viewController2, animated: true, completion: nil)
and, if you had embed navigation controller then
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.navigationController?.pushViewController(viewController2, animated: true)
In swift 4.2 answer for those who want this answer without navigation in modally and in swift updated versions.
let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "mainViewController")
self.present(controller, animated: true, completion: nil)
UIViewController *initialHelpView = [[UIStoryboard storyboardWithName:#"StoryBoard_IDentifier" bundle:nil] instantiateViewControllerWithIdentifier:#"ViewController_Identifier"];
[self presentViewController:initialHelpView animated:YES completion:nil];