I want to present SecondVC from FirstVC and make SecondVC have a rightBarButtonItem called Close which calls an #objc function which dismisses SecondVC.
Also, I want to change secondVC's title from firstVC:
This is how I present SecondVC from the FirstVC:
let secondVC = AdviceDetailsViewController()
secondVC.modalPresentationStyle = .fullScreen
secondVC.title = "Example" //Value of type 'UINavigationController' has no member 'myTitle'
self.present(secondVC, animated: true)
Code for navigationBar in secondVC:
public var myTitle: String = ""
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .gray
self.title = myTitle
self.navigationController?.navigationBar.barTintColor = UIColor.orange
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(closeDetails))
}
#objc func closeDetails() {
self.dismiss(animated: true, completion: nil)
}
No Navigation bar is visible in secondVC, the only visible thing is gray background color.
What should I change? I am doing everything programmatically in this app.
Use this method to show second VC
let nc = UINavigationController(rootViewController: AdviceDetailsViewController())
nc.modalPresentationStyle = . fullScreen
self.present(nc, animated: true)
Regarding your Title issue,
Value of type 'UINavigationController' has no member 'myTitle' Yes its true, because we define myTitle variable on SecondVC,So we assign myTitle like this
let vc = SecondViewController()
vc.myTitle = "My Title"
let nc = UINavigationController(rootViewController: vc)
nc.modalPresentationStyle = . fullScreen
self.present(nc, animated: true)
Related
How to present UIViewController() inside of UINavigationController ? My controller presenting not fullscreen. I want my app to look like this enter image description here, but it is end up like this enter image description here
I making app without Storyboard(Programmatically)
let customVC = UIViewController()
customVC.view.backgroundColor = .green
customVC.modalPresentationStyle = .fullScreen
let navVC = UINavigationController(rootViewController: customVC)
self.present(navVC, animated: true, completion: nil)
let customVC = DestinationController()
let navVC = UINavigationController(rootViewController: customVC)
// this line overFullScreen
navVC.modalPresentationStyle = .overFullScreen
// for more style
navVC.modalTransitionStyle = .crossDissolve
self.present(navVC, animated: true, completion: nil)
your class where you want to go or present
class DestinationController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
I have created a navigation controller. I added a plus button to the top right of the navigation controller which opens up a new view controller. However, I want the navigation bar to show up on the new screen as well. But it's not showing. What am I doing wrong, or what am I missing? Here is my code:
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var toDoListTextView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Setting the Title for the nav bar
title = "To Do List"
configureNavigationItems()
}
private func configureNavigationItems(){
/* Adding a button on the top right of the screen
* in the navigation bar/controller.
*/
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .add,
target: self,
action: #selector(didTapPlus)
)
}
#objc func didTapPlus(){
let vc = storyboard?.instantiateViewController(identifier: "makeTask") as! makeTaskViewController
vc.modalPresentationStyle = .fullScreen
vc.title = "Create a Task"
vc.view.backgroundColor = .black
vc.navigationItem.leftBarButtonItem = UIBarButtonItem(
title: "Back",
style: .done,
target: self,
action: nil)
present(vc, animated: true)
}
}
Storyboard
Click Here To See Picture
Demo of App
enter image description here
Each modally-presented view controller gets a brand-new view hierarchy. This means that if you want your MakeTaskViewController to have a navigation bar, you'll need to manually add a navigation controller.
class ViewController: UIViewController {
// ... your code ...
#objc func didTapPlus() {
let vc = storyboard?.instantiateViewController(identifier: "makeTask") as! MakeTaskViewController
vc.modalPresentationStyle = .fullScreen
vc.title = "Create a Task"
vc.view.backgroundColor = .black
vc.navigationItem.leftBarButtonItem = UIBarButtonItem(
title: "Back",
style: .done,
target: vc, /// make sure to set `target` as the new view controller
action: #selector(vc.dismissMe))
/// wrap the new view controller in a navigation controller (this adds the top bar)
let navigationController = UINavigationController(rootViewController: vc)
/// so you can actually see the `Create a Task` text,
navigationController.navigationBar.barStyle = .black
present(navigationController, animated: true)
}
}
/// side note: `MakeTaskViewController` and other classes should be Capitalized
class MakeTaskViewController: UIViewController {
#objc func dismissMe() {
self.dismiss(animated: true)
}
}
Result:
Try this!
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var toDoListTextView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Setting the Title for the nav bar
title = "To Do List"
configureNavigationItems()
}
private func configureNavigationItems(){
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .add,
target: self,
action: #selector(didTapPlus)
)
}
#objc func didTapPlus(){
let vc = storyboard?.instantiateViewController(identifier: "makeTask") as! makeTaskViewController
vc.modalPresentationStyle = .fullScreen
vc.title = "Create a Task"
vc.view.backgroundColor = .black
vc.navigationItem.leftBarButtonItem = UIBarButtonItem(
title: "Back",
style: .done,
target: self,
action: nil)
navigationController?.pushViewController(vc, animated: true)
//present(vc, animated: true)
}
}
😊
I am facing a problem, because there are two ways how do I display my ViewController.
First way is performSegue(withIdentifier: "segue", sender: self)
It works fine because then I have this back button in my navigationItem:
Then I use this code to present the same ViewController (from another viewController than in the first case):
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = storyboard.instantiateViewController(withIdentifier: "navViewController") as! UINavigationController
let vc = navVC.topViewController as! ViewController
self.present(navVC, animated: true, completion: nil)
but then I do not have any back button in my ViewController.
My question is: How can I keep my backButton (exactly how it is) when I use this function: performSegue(withIdentifier: "segue", sender: self) , but add button (can look different) when I use this function: self.present(navVC, animated: true, completion: nil)
Note: In my case 1 my segue is connected right to ViewController , but in case 2 I present UINavigationController and ViewController is embed in in this UINavigationController.
Edit: I tried this code, but it always prints: "1.........":
if self.presentingViewController != nil {
print("1..........")
} else if self.navigationController?.presentingViewController?.presentedViewController == self.navigationController {
return print("2.........")
} else if self.tabBarController?.presentingViewController is UITabBarController {
return print("3........")
}
And also this code prints:"Else.............." :
let isPresentingInAddMealMode = presentedViewController is UINavigationController
if isPresentingInAddMealMode {
print("FirstOption......................")
} else {
print("Else......................")
}
If you need more info just let me know.
Thank you very much.
you need to check presentedViewController and add back button as below .
if ([self presentedViewController]) {
// add your back button item here
}
Try this:
let viewController = storyboard?.instantiateViewController(withIdentifier: "Login") as! LoginView
let customNavigation = UINavigationController(rootViewController: viewController)
customNavigation.navigationBar.tintColor = UIColor.black
self.present(customNavigation, animated: true, completion: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Login"
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.backButton))
}
func backButton() {
self.dismiss(animated: true, completion: nil)
}
Try to set Image in UIBarButtonItem
I have solved it!! I put restorationIdentifier to my root navigationController and then I just check if that is navigationController with my restorationIdentifier like this:
if self.navigationController?.restorationIdentifier == "navViewController"{
let leftItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(goBack))
self.navigationItem.leftBarButtonItem = leftItem //Adds item if id is navViewController
}else{
self.navigationItem.leftBarButtonItem = nil //removes it and keeps "<Back"
}
I have set the navigation programmatically
in a view controller I set a backbutton and only change the title as per documentation.
however clicking on the back button in the child controller does nothing.
what did I miss?
> in a viewController
let backItem = UIBarButtonItem()
backItem.title = ""
navigationItem.backBarButtonItem = backItem
self.navigationController?.pushViewController(secondViewController, animated: true)
You have to add a target and action to the button,
let backItem = UIBarButtonItem()
backItem.title = ""
backItem.target = self
backItem.action = #selector(back)
navigationItem.backBarButtonItem = backItem
self.navigationController?.pushViewController(secondViewController, animated: true)
and then implement the back() function.
func back() {
// If your view controller was presented by a navigation controller
self.navigationController?.popViewControllerAnimated(true)
// If your view controller was presented modally
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
I need your help.
I'm developing a app without Storyboard (using xib), in my project I have two views controllers.
In the main view (after a login) I need to put a button on top of the view to open a left side menu.
This is how I call the MainView on LoginViewController
#IBAction func logar(){
let vc = MainViewController(nibName: "MainViewController", bundle: nil)
self.navigationController!.pushViewController(vc, animated: true)
}
I imported SWRevealViewController, and this is my MainViewController:
import UIKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//hide BackButton
self.navigationItem.hidesBackButton = true
//image
var image = UIImage(named: "menuBtn")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//lefBarButonItem
let newBackButton = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(MainViewController.openMenu))
self.navigationItem.leftBarButtonItem = newBackButton;
}
func openMenu(){
let frontViewController = LeftMenuTableViewController()
let rearViewController = MainViewController()
//create instance of swRevealVC based on front and rear VC
let swRevealVC = SWRevealViewController(rearViewController: rearViewController, frontViewController: frontViewController);
swRevealVC.toggleAnimationType = SWRevealToggleAnimationType.EaseOut;
swRevealVC.toggleAnimationDuration = 0.30;
//set swRevealVC as rootVC of windows
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window!.rootViewController = swRevealVC
}
}
When I press the button the LeftMenu occupy all the view, not like a partial view.
Any help would be appreciated. Thank you!