I have a question regarding the navigation bar.
As far as I understand from iOS: A view controller opened by a segue inherits the navigation bar of the parent view controller. Is this correct so far?
Is there a view controller within a stack "owns" the navigation bar in a complex segue stack (e.g. TableViewController that opens a TabBarController that opens ...)?
I very often run into the problem that I don't know where to get the actual navigation item in order to set the title or a bar button item.
In this case, I have the following controllers:
TabBarController
EventPostsViewController -> To display a list of posts, is a tabbed view within the TabBarController
CreatePostViewController -> To write a new post
So within the EventPostsViewController I can do this (and it works):
class EventPostsViewController: UITableViewController {
...
override func viewWillAppear(animated: Bool) {
...
// This solution works, but only for EventPostsViewController
self.tabBarController?.navigationItem.title = "text"
But within the CreatePostViewController, which is opened by a segue via EventPostsViewController, neither of this solutions work.
class CreatePostViewController: UIViewController {
...
override func viewWillAppear(animated: Bool) {
...
// Neither of these solutions works
self.navigationItem.title = "Text"
self.tabBarController?.navigationItem.title = "Text"
self.navigationController?.navigationItem.title = "Text"
How do I get the actual navigation bar/navigationItem?
Stupid simple mistake I repeat every time :)
I forgot to link my custom CreatePostViewController with the view controller using the interface builder.
This code now works:
class CreatePostViewController: UIViewController {
...
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.setNavigationBarHidden(false, animated: false)
// Set title
self.navigationItem.title = "Write Post"
// Add Submit button
var submitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "submitPost:")
self.navigationItem.rightBarButtonItem = submitButton
}
...
}
Related
I have an existing navigation controller delegate that places a menu button on each view controller in the app.
class MyNavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let navItem = viewController.navigationItem
let menuBtn = MyCustomMenuButton()
...
navItem.setRightBarButton(menuBtn, animated: false)
}
This works great...I get a menu button in the nav bar for each view. But for some views, I'd like to add another button on the right next to the menu button, so I added this:
class CustomViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let newButton = UIBarButtonItem(title: "(+)", style: .plain, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = newButton
}
}
But this has no effect. The menu button is still there but the new button is not added. How should this be done then?
But this has no effect
Yes, it does. But you don't have time to see the effect. You are saying the same thing twice, because setRightBarButtonItem is the same as rightBarButtonItem =... So whichever one you say second, that is the one that is ultimately obeyed; either way, it rips the existing right bar button item out and replaces it with the other one.
If the goal is to have multiple right bar button items, that is what the rightBarButtonItems is for (notice the plural). You can call setRightBarButtonItems instead (again, notice the plural). Looking to see whether there is already a right bar button item and taking that into account is up to you, of course. There is no magical append method.
Very stuck on an issue
I have a tab bar controller within a navigation controller
The first tab has a calendar on it (which is basically a collection view)
I am trying to make a rightbarbuttonitem to scroll to todays date
I can only seem to create the button within the tabbarcontroller
The function that I call then calls one in the CalendarViewController
but it doesn't seem to work
What is the correct way to implement a bar button item within a tabbarcontroller?
In the tabbarcontroller I have...
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(goToToday))
#objc func goToToday() {
print("pressed")
CalendarViewController().goToToday()
}
In the CalendarViewController I have...
#objc func goToToday(_ animate: Bool = true) {
print("tapped")
calendarView.scrollToDate(Date(),animateScroll: animate)
calendarView.selectDates([Date()])
title = "Test"
}
The function works if I call it from a button in CalendarViewController. And I get both print commands when called from the navigation controller but that's all
You should call gotToToday on the instance you're using as the first tab and not on a new instance as you've done here. Here's what you need to do in gotToToday in TabBarController:
#objc func goToToday() {
print("pressed")
if let calendarViewController = viewControllers[0] as? CalendarViewController {
calendarViewController.goToToday()
}
}
I have multiple view controllers embedded inside a navigation controller. I have an option that changes the language of the user interface. When user chooses French, the user interface should update itself with French language. All elements are updated with French as I expected, except the button that takes user back to the previous view controller (as you can see in the screenshot - "List of Events").
This is the function I call to update UI :
func updateView() {
DispatchQueue.main.async {
// CurrentSelection.LanguageUI holds a reference to currenly selected language object
navigationItem.title = CurrentSelection.languageUI.event_singular
navigationController?.title = CurrentSelection.languageUI.listOfEvents <<- this line doesn't work
}
}
The navigationController?.title holds "List of Events" string and the assignment statement seems to work. It just that the UI isn't updated with the new title value. Where am I doing wrong?
It's the previousVC that decides what it's own back button will be.
So set this on the previousVC in viewDidLoad
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back title" style:UIBarButtonItemStylePlain target:nil action:nil];
If this isn't set then the back button will be the .title of the previous VC.
-
In your situation you should have some kind of language change notification that the previous VC can listen for and know to update its own title.
One way to do it is to implement UINavigationControllerDelegate and set the back bar button item there:
class YourViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
}
// MARK: - Navigation controller delegate
public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let item = UIBarButtonItem(title: "Custom title", style: .plain, target: nil, action: nil)
viewController.navigationItem.backBarButtonItem = item
}
}
Something to be aware of is that this needs to be implemented in the view controller which pushes the next view controller. So you'll be changing the back bar button item of the view controller that willShow.
Those view controllers in the navigation stack won't change atomatically.
So you have to use Notification center and add observers to the previous view controllers and change the Title of the navigation bar with selected language when it is called.
When language is changed you have to post the change.
Then it will change the title to french so does the back button will
change.
I am trying to create a Sidebar menu. The sidebar menu is shown in the image below. This sidebar menu works fine.
However, my problem is that when I go from the table view controller to the detail view controller (titled smif in the image), and click back, my sidebar menu no longer works. I am using SWRevealViewController to make the sidebar menu work.
Code available upon request. Thank you!
Did you set target action for menu button as revealToggle: (api exposed by SWRevealViewController) in your detail view controller.
Example: Swift3.0
if revealViewController() != nil {
sideBarButton.target = revealViewController()
sideBarButton.action = #selector(SWRevealViewController.revealToggle(_:))
}
UPDATE:
Add below code in your classes:
1- In all your view controllers add self.revealViewController().tapGestureRecognizer() just after where you are setting actions as revealToggle()
2- In MenuController add below code:
override func viewWillAppear(_ animated: Bool) {
if (self.revealViewController()) != nil{
self.revealViewController().frontViewController.view.isUserInteractionEnabled = false
}
}
override func viewDidDisappear(_ animated: Bool) {
if let revealVC = self.revealViewController() {
revealVC.frontViewController.view.isUserInteractionEnabled = true
}
}
By removing the navigation controller in the detailViewController, it was able have a sidebar in both the detailViewController and the TableViewController
I want to know if its possible to remove the navigation bar back button text from an inherited navigation bar. Presently my navigation bar shows "< ControllerName". I want to simply show the "<" back icon. I would also like to know how to show "< Back", and how to remove it completely.
I understand I could do this by adding a bar button item to the storyboard, however is there an easier way to do it?
Note, this code does not work:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
You better custom back button for this task.
but You also can do it in other ways. Ex: You have ViewController1, and ViewController2 (You push ViewController2 from ViewController1)
ViewController1
public class ViewController1: UIViewController {
override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = "viewcontroller1 title"
}
}
ViewController2
class ViewController2: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// get previous view controller and set title to "" or any things You want
if let viewControllers = self.navigationController?.viewControllers {
let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
previousVC?.title = "" // or previousVC?.title = "Back"
}
}
}
I think this will work for you.
self.navigationItem.hidesBackButton = true
Solution suggested by #Prashant will remove the back button from navigation bar.
To remove the title, use following:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)