Navigation Item title issue - ios

I've an UITabBarController connected with an UINavigationController related with an UITableView and finally connected with an UIViewController.
I've connected programmatically the UITableView with the UIDetailViewController using that code:
...
navigationController?.pushViewController(detail!, animated: true)
In my UIDetailViewController if I try to change the navigationItem.title like that:
self.navigationController!.navigationItem.title = "Hello"
Nothing happens. The navigation item doesn't change title.

You can change title of navigationItem title using this :-
self.navigationItem.title = "YourTitle";
or other way is :-
In UIDetailViewController write this line of code :
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "ok"
}

Related

Navigation Item Title not Showing

I have following code in class homeVC: UICollectionViewController to display the title in the navigation bar and it works as expected.
override func viewDidLoad() {
super.viewDidLoad()
.
.
.
self.navigationItem.title = PFUser.current()?.username?.uppercased()
.
.
}
But when I clicked on the button to go to another view controller and then came back to homeVC view controller self.navigationItem.title is not displaying anything. I'mm wondering why that happened?
The following code is to go to another view controller
#objc func followersTap() {
category = "followers"
// make references to followersVC
let followers = self.storyboard?.instantiateViewController(withIdentifier: "followersVC") as! followersVC
// present
self.navigationController?.pushViewController(followers, animated: true)
}
This is the view controller to go to , and in this view controller i don't want to show the title from previous view controller so I'm using self.navigationController!.navigationBar.topItem!.title = "" to make it empty and when I go back to previous view controller the title is also empty, why?
class followersVC: UITableViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController!.navigationBar.topItem!.title = ""
self.tableView.reloadData()
}
I am trying to not show the first view controller title on second view controller top left corner where back button is and keep first and second view controller title shows.
If your plan is to not show the title from a previous view controller, the solution is like this:
Change:
self.navigationItem.title = PFUser.current()?.username?.uppercased()
to:
self.title = PFUser.current()?.username?.uppercased()
And change:
self.navigationController!.navigationBar.topItem!.title = ""
to:
self.title = ""
Try to move your self.navigationItem.title = PFUser.current()?.username?.uppercased() in viewDidAppear(), this should help

Setting navigation item title issue.. why my navigation item seems many?

I want to change the title of navigation bar but it seems somewhat confusing..
I did self.navigationItem.title = "ipsum" or self.navigationController?.navigationBar.topItem?.title = "ipsum" and so on.. but it fails..
It only succeeds changing the title when trying self.title = "ipsum" but it is not what I want because it causes tabBarItem's title to change too.
I tried debugging and found out strange result.
as you see the picture above.. there seems to be more than two navigationBar so when self.navigationItem.title = "ipsum" it actually changes some other or invisible navigationItem's title so that I can see from the log.
But the UI stays "lorem". Tell me if you figure something out! What am I missing?
EDIT 1:
Main.storyboard
Menu.storyboard
I put NavigationController in TabBarController and refactored NavigationController to another storyboard reference.
self.navigationItem.title = "ipsum" is called in the viewDidLoad() function.
Now View controllers are:
ViewController1 attached to item1:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title="View1"
}
ViewController2 attached to item2:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title="View2"
}
Result:
I used self.navigationItem.title="????". It worked for me:)
You should always use just
navigationItem.title = ""
because each controller has it's own navigationItem and if you trying to set title like this - self.navigationController?.navigationItem.title you possible set it to wrong place.

UISearchbar (UISearchResultsUpdating) with Segue is still visible

I have a problem with a UISearchBar. When ill search some Text in combination with an UITableView, and ill click on one result Cell, the UISearchBar is still visible in the next View Controller. If ill go back (with Segues) - the UISearchbar is still there (with the Keyword)
So after ill click on one result, ill get (in the next View Controller):
Ill use it this way:
class ...: UITableViewController, UISearchResultsUpdating {
var filterSearchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
filterSearchController.searchResultsUpdater = self
filterSearchController.hidesNavigationBarDuringPresentation = false
filterSearchController.dimsBackgroundDuringPresentation = false
filterSearchController.searchBar.searchBarStyle = .Minimal
filterSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = filterSearchController.searchBar
Any ideas what could be a problem?
You need to dismiss the UISearchController yourself before transitioning to the next view controller with:
filterSearchController.active = false

The NavigationItem's TintColor can not be set

My Situation:
There are a View_A (UICollectionViewController&UICollectionViewCell) and View_B (UIViewController). I want to switch to the View_B when I touched one of cell in the View_A with Segue in the StoryBoard.
In the StoryBoard, I connected View_A and View_B with the Push Segue which identifier is SegueToView_B.
And the function of switchViews just worked fine.
My Problem:
With the Push Segue, I do not need to add a BackButton (NavigationItem) to turn back to the View_A, because there is a 'NavigationItem' be crated automatically by system. And I tried other type segues, like Modal, Popover, and the NavigationItem was not created automatically. I want to ask why?
I want to set the specific color, not the default blue, for that NavigationItem which be created by system automatically, but I failed to find it. After that I just set the color in the prepareForSegue(), but it did not work. Please tell how to set the specific color for it?
My Code:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.collectionView?.setPresenting(true, animated: true, completion: nil)
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
self.selectedCard = delegate.otherCards[indexPath.row]
self.performSegueWithIdentifier("SegueToView_B", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
if identifier == "SegueToView_B" {
let myOtherCardViewController = segue.destinationViewController as? View_BViewController
myOtherCardViewController!.otherCard = self.selectedCard
myOtherCardViewController!.navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor() // Failed to work!!!
myOtherCardViewController?.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor() // Failed to work, too!!!
}
}
}
Thanks for your help.
Ethan Joe
To set the tintColor for that navigation bar:
myOtherCardViewController.navigationBar.tintColor = .whiteColor()
Why there is no Navigationbar when you use the Modal or PopOver? Because thats how Modal and Popover work! You have to create another Navigation controller for the view you are connecting with the Modal segue, like this:
Another technique I am using is, to create a single NavigationController class, and set all the desired properties (color, font etc.) and then link all the NavigationControllers in the Storyboard to that NavigationController class.
With that you wont have to reconfigure every NavigationController.
Your Solution
You can set back button hidden in View_B controller in viewDidLoad method like this.
class View_BViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true;
// Do any additional setup after loading the view.
}
}
To set tint color, you have to create subclass of UINavigationController, and assign that class to your UINavigationController in UIStoryboard
You subclass will look like this, to set tint color,
class navigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
//self.navigationBar.barStyle = UIBarStyle.Default
self.navigationBar.tintColor = UIColor.redColor()
// Do any additional setup after loading the view.
}
//Other stuff
}
May this help you!!

Can't get any navigation item in iOS

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
}
...
}

Resources