How can I set barButtonItem in AppDelegate? Now I have this code:
func presentDetailViewController(_ hallID: String) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = UINavigationController()
let newDetailVC = storyboard.instantiateViewController(withIdentifier: "newDetailVC") as! NewDetailTableViewController
newDetailVC.hallID = hallID
navVC.viewControllers = [newDetailVC]
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
navVC.navigationItem.setRightBarButton(backItem, animated: true)
window?.rootViewController = navVC
window?.makeKeyAndVisible()
}
#objc func goToMainVC() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = UINavigationController()
let mainVC = storyboard.instantiateViewController(withIdentifier: "MainVC") as! PhotoStudiosViewController
navVC.viewControllers = [mainVC]
window?.rootViewController = navVC
window?.makeKeyAndVisible()
}
let backItem = UIBarButtonItem(title: "Назад", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
navVC.navigationItem.setRightBarButton(backItem, animated: true)
This line not helped me and barButtonItem still not appear.
How can I create backButton with my action func goToMainVC?
You don't set the navigation item to navigation controller rather you set the navigation item to the navigation bar of viewController
so change your code as
let newDetailVC = storyboard.instantiateViewController(withIdentifier: "newDetailVC") as! NewDetailTableViewController
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
//navVC.navigationItem.setRightBarButton(backItem, animated: true) this is the issue
newDetailVC.navigationItem.setRightBarButton(backItem, animated: true)
Hope this helps
Related
I can not succeed in presenting a UI view and keep the Tab bar in Swift. It does successfully present but there is no tab bar. It appears that presenting a UIView removes the tab bar. Here is my code.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goMessages))
Here is what I use.
class MessagesController: UITableViewController {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goMessages))
let image = UIImage(named: "new_message_icon")
navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(handleNewMessage))
checkIfUserIsLoggedIn()
tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
// observeMessages()
}
#objc func goMessages() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Messages = storyBoard.instantiateViewController(withIdentifier: "Messages") as! MainMSGController
self.present(Messages, animated:true, completion:nil)
You have to push the view controller inside your navigationViewController.
But if you do not have one you should embed your MessagesController inside UINavigationViewController.
(inside your Main.storyboard, select MessagesController then from menu Editor -> Embed in -> Navigation Controller)
and then:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Messages = storyBoard.instantiateViewController(withIdentifier: "Messages") as! MainMSGController
navigationController?.pushViewController(viewController: Messages, animated: true)
to close it you can:
navigationController?.popViewController(animated: true)
You can't present view controller and keep the tabs. You can only push the view controller if you want to do that. There are some other things required but at the first glance they seem to be implemented properly in your code.
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("HomeTabController")))
self.navigationItem.leftBarButtonItem = backButton
The above code is the creation of button in navigationController but I can create a button cannot write a specific view controller to open.
I have tried with popViewController and popToRootViewController action, need a specific code for opening a particular viewController in swift, with the help of the particular viewController storyboard id and viewcontrollername.
You need to add func name in #selctor() (Swift 4 version)
let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goToViewController(_:)))
self.navigationItem.leftBarButtonItem = backButton
You need to create a func.
#objc func goToViewController(_ sender: UIBarButtonItem) {
//write code here to open a view controller
let storyboard = "Main"
let viewControllerIdentifier = "HomeVC"
let viewController = UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: viewControllerIdentifier) as! HomeVC
//push/present "viewController"
}
in this code just replace ChatVC name to your viewcontroller name
#objc func goToViewController(_ sender: UIBarButtonItem) {
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ChatVC.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
} else {
self.navigationController?.popViewController(animated: true)
}
}
}
I'm relatively new at iOS. I'm struggling, for a while now with this issue. I don't know how to set the logic for presenting popUp when user taps on right bar button item. Basically, it should look like this:PopUp I searched through Google, but I didn't have any luck. I would appreciate if someone would help me with some code.
//My VC
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let rightBarButtonItem = UIBarButtonItem(title: "Share", style: .plain, target: self, action: #selector(clickShare))
rightBarButtonItem.tintColor = UIColor.black
navigationItem.rightBarButtonItem = rightBarButtonItem
// navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: UIBarButtonItemStyle.plain, target: self, action: #selector(clickShare))
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "backArrow"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(goBack))
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "OpenSans", size: 14)! ], for: .normal)
}
//MARK: - Actions
#objc func goBack() {
navigationController?.popViewController(animated: true)
}
#objc func clickShare() {
//this is where the logic should go
}
//This is my storyboard:
Storyboard
Add following action,
#IBAction func yourButtonClickAction(sender: UIBarButtonItem) {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("YourViewController") as! UIViewController
vc.modalPresentationStyle = UIModalPresentationStyle.Popover
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.barButtonItem = sender
popover.delegate = self
presentViewController(vc, animated: true, completion:nil)
}
Add this action to you barButtonItem.
'YourViewController' = controller consisting of Public, followers options.
func myPlaceViewMoreWasPressed() {
print("myPlaceViewMore was pressed")
let vc = MyPlacesViewController(nil)
let nav = UINavigationController(rootViewController: vc)
let dismissButton = UIBarButtonItem.init(title: "Dismiss", style: .plain, target: self, action: #selector(dismissButtonWasPressed))
nav.navigationItem.leftBarButtonItem = dismissButton
self.present(nav, animated: true, completion: nil)
}
Should be very straightforward... instantiate a ViewController, contain it in a navigation controller, init a UIBarButtonItem and set it the leftBarButtonItem of the nav. I've also tried it like this:
func myPlaceViewMoreWasPressed() {
print("myPlaceViewMore was pressed")
let vc = MyPlacesViewController(nil)
let nav = UINavigationController(rootViewController: vc)
let dismissButton = UIBarButtonItem.init(title: "Dismiss", style: .plain, target: self, action: #selector(dismissButtonWasPressed))
nav.navigationItem.setLeftBarButton(dismissButton, animated: true)
self.present(nav, animated: true, completion: nil)
}
But the button will not appear.
You need to update the navigationItem of the view controller, not the navigation controller. I would do that before setting the view controller as the root controller.
func myPlaceViewMoreWasPressed() {
print("myPlaceViewMore was pressed")
let vc = MyPlacesViewController(nil)
let dismissButton = UIBarButtonItem.init(title: "Dismiss", style: .plain, target: self, action: #selector(dismissButtonWasPressed))
vc.navigationItem.leftBarButtonItem = dismissButton
let nav = UINavigationController(rootViewController: vc)
self.present(nav, animated: true, completion: nil)
}
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"
}