Reputation:
How can I make this button open a new navigation controller? I want it to force open a new controller from the right.
I need to do this all programmatically not using the storyboard.
@objc func buttonAction(sender: UIButton!) {
let loginDetailController = UIViewController()
navigationController?.pushViewController(loginDetailController, animated: true)
print("Button tapped")
}
Here is the code that makes the view controller I am editing pop up when a user is not logged in. This code is in the rootview controller.
func checkIfUserIsLoggedIn() {
if Auth.auth().currentUser?.uid == nil {
perform(#selector(handleLogout), with: nil,
afterDelay: 0)
}else{
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: {(snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.navigationItem.title = dictionary["name"] as? String
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}
},withCancel: nil)
}
}
override func didMove(toParentViewController parent: UIViewController?) {
checkIfUserIsLoggedIn()
}
@objc func handleLogout() {
do {
try Auth.auth().signOut()
} catch let logoutError {
print(logoutError)
}
let loginController = TwoLoginController()
present(loginController, animated: true, completion:
nil)
}
}
Here is where I added the dismiss of navigation controller.
func handleLogin() {
guard let email = emailTextField.text, let password = passwordTextField.text else{
print("invalid form")
return
}
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if error != nil {
print(error!)
return
}
//logged in
self.dismiss(animated: true, completion: nil)
self.navigationController?.dismiss(animated: true, completion: nil)
}
}
New Approach?
import UIKit
import Firebase
class TwoLoginController: UINavigationController {
Upvotes: 0
Views: 678
Reputation: 2216
After a discussion and getting more of the picture. This is how you should programmatically present a ViewController
inside of a new NavigationController
@objc func handleLogout() {
do {
try Auth.auth().signOut()
} catch let logoutError {
print(logoutError)
}
let loginController = TwoLoginController()
let navVC = UINavigationController(rootViewController: loginController)
present(navVC, animated: true, completion: nil)
}
Then to destroy the navigation controller at the end it should be something like this:
self.navigationController?.dismiss(animated: true) {
//
}
Do this when the log in is completed when you need to segue to the next view.
Upvotes: 0
Reputation: 2216
With Storyboards
First create a new file that is a custom viewController class. For this example we will call it YourCustomViewController. Then ( go to your storyboard and add a new viewController to the storyboard. Select that ViewController and give it an ID and set its class. After that is done put the following code in your function :
let controller = self.storyboard!.instantiateViewController(withIdentifier: "Your View's Identifier") as! YourCustomViewController
self.navigationController!.pushViewController(controller, animated: true)
Without Storyboards :
In your AppDelegate
// put the following code in your appDelegate didFinishLaunchingWithOptions function
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
UINavigationBar.appearance().barTintColor = UIColor.rgb(230, green: 32, blue: 31)
// get rid of black bar underneath navbar
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
application.statusBarStyle = .lightContent
let statusBarBackgroundView = UIView()
statusBarBackgroundView.backgroundColor = UIColor.rgb(194, green: 31, blue: 31)
window?.addSubview(statusBarBackgroundView)
window?.addConstraintsWithFormat("H:|[v0]|", views: statusBarBackgroundView)
window?.addConstraintsWithFormat("V:|[v0(20)]", views: statusBarBackgroundView)
In your Action to present the view
// this code belongs in your button's action
let dummyViewController = UIViewController()
dummyViewController.view.backgroundColor = .white
dummyViewController.navigationItem.title = "TEST"
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController?.pushViewController(dummyViewController, animated: true)
Upvotes: 1