So I have a basic view controller with a navigation bar, this is view controller B, so I'm performing a segue to go here, and I'm trying to change the title like this:
override func viewDidLoad() {
debugPrint(self.selectedName)
super.viewDidLoad();
self.navigationItem.title = "A NEW TITLE"
}
But it doesn't do anything
To set Title on NavigationBar simply we can do by below code
self.title = "Your Title"
For Swift 3:
If you want to set just the navigation title without using a UINavigationController then make an outlet of the navigation item as
#IBOutlet weak var navItem: UINavigationItem!
and then in viewDidLoad() write
navItem.title = "ANY TITLE"
There are plenty of methods which you can follow to achieve this. Here is few of those.
First things first.
If you are ready to "Embed in" a navigation controller or if you have already one then you can access that using following code.
self.navigationController?.navigationBar.topItem?.title = "Your Title"
Now for more customization:
Place a UINavigationBar object on ViewController scene and add constraints to it.
Make an outlet of newly placed UINavigationBar like as follows -
#IBOutlet weak var orderStatusNavigationbar: UINavigationBar!
Now set the title using the outlet.
orderStatusNavigationbar.topItem?.title = "Your Title"
All this above code are in Swift 3 but will work on lower versions of Swift also(atleast on Swift 2.0)
Hope this helped.
Swift 4 / XCode 10
Add new NavigationBar -> Drap and Drop it to your view
Press CTRL to add new Outlet Action
Example : #IBOutlet weak var main_navbar: UINavigationBar in ViewController class.
Then set the title : main_navbar.topItem?.title = "YOUR TITLE"
This solution worked for me, hope it does for you too.
- rust
Ok,
So you need to embed the whole thing in a Navigation Controller first, and then make that navigation controller as the initial controller.
Select your storyboard, click the first controller then click this -
Then you remove the navigation bar you set(put) over the last controller named "title".
The reason this didnt work, as you are trying to change the title of the navigation controller's navigation bar, but it doesnt have it, hence it cant change it.
Related
I'm trying to combine a Sidemenu and a Tapbarmenu using Xcode's Storyboards.
I took the template from here: https://johncodeos.com/how-to-create-a-side-menu-in-ios-using-swift/
Then, I modified the Home View to have a tap bar :
The first issue that I got is that I lost the SideMenu button :
.
Furthermore, you can notice that the Topbar got really thick and the View is not centered.
To fix the first issue, I tried to create a new cocoapod file for the new navigation controller in order to get the SideMenu button working again :
class MenuViewController: UITabBarController {
#IBOutlet var sideMenuBtn: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Menu Button Tint Color
navigationController?.navigationBar.tintColor = .white
sideMenuBtn.target = revealViewController()
sideMenuBtn.action = #selector(revealViewController()?.revealSideMenu)
}
}
I assigned the new class to the Navigation controller but the sideMenuBtn won't appear on the Sidemenu button.
I don't know if my explanations were very clear but don't hesitate to ask me for more information.
Thx,
Julien
How do I programmatically hide or show the START SESSION button in a scene's view controller that navigates to the next scene?
create an outlet to your button in the view controller class:
#IBOutlet weak var startButton: UIButton!
and then in whichever function you want you can show hide it, I assume you want it hidden by default So in view did load you can do
override func viewDidLoad()
{
startButton.isHidden = true
}
and then show it somewhere else
func doSomethingAndShowButton()
{
// Do some other stuff
...
// Show the button
startButton.isHidden = false
}
#Doug Null To connect the #IBOutlet. First, follow what #AngrayDuck said. After that do as following:
Go to the storyboard.
Open "Connection inspector" on the right side. Can open through shortcut "Command + Option + 7".
Follow the steps highlighted in the below image.
Hope this helps.
I have FormViewController that I made full programmatically with Eureka form builder (link). I dont have view controller in storyboard for that. Class name of that view controller is NewPasswordVC. When I click od add bar button I open NewPasswordVC with this code:
let newPasswordVC = NewPasswordVC()
self.navigationController?.pushViewController(newPasswordVC, animated: true)
I open NewPasswordVC but when I go back in root view controller my bottom toolbar disappear. Why? How to fix this?
This is storyboard:
This is my problem in gif:
Can't speak about Eureka specifically, but chances are the UIViewController being pushed in has hidesBottomBarWhenPushed set to true.
So I would look into setting it to false, which can be done programmatically.
The solution to my problem I found here: link
override func willMove(toParent parent: UIViewController?){
super.willMove(toParent: parent)
if parent == nil{
self.navigationController?.isToolbarHidden = false
}
}
I'm trying to be clever about setting all title properties of the the "Back" buttons in a UINavigationController so that I don't have to do self.navigationController.navigationBar.backButtonItem.title = "Back" everywhere or subclass a UINavigationController and set it everywhere, so I've created this extension:
extension UINavigationItem {
open var backBarButtonItem: UIBarButtonItem? {
get {
return self.backBarButtonItem
}
set {
newValue?.title = "Back"
backBarButtonItem = newValue?
}
}
}
But it says 'backBarButtonItem' used within its own type.
Has anybody done this before or can think of a way to make it work?
You are getting this error because you cannot create a variable with the name which is similar to those variables which are defined in the SDK.
You can't override the existing functionality
Like in your case you are naming it as backBarButtonTitle which is defined as open var backBarButtonItem: UIBarButtonItem? in UINavigationBar class of UIKit
As it is mentioned in doc of Apple
Extensions can add new functionality to a type, but they cannot
override existing functionality.
Please follow this Screen shot Image then run your project . I think you can solved your problem easily :)
I have 1 tab bar controller in storyboard and 1 UIViewController associated with it. I would like to re-use the same UIViewController in order to create second item in tab bar. When I am creating second relation from tab bar to view controller I need to specify 2 different items names. How can I re-use same view controller and set different items names from storyboard? If not possible to do it in storyboard, then do I have to rename each in tab bar controller class or there is better way?
I was going to provide different data to view controller in prepareforsegue.
UPDATE:
little more details and clarification
In above screenshot marked VC at the moment is reachable a) directly from tab, b) through 3 transitions. I want to add another DIRECT relation to initial tab bar, just like in case of "a".
I can give you a little tweak for that and at least that worked for me.
Drag a tabbarcontroller and associated tab item view controllers to
your storyboard. Name them as you like.
Create an extra view controller that you want to reuse from your storyboard.
Add container views to each tab item view controllers and remove their default embedded view controllers.
Create embed segue from each tab item controller to your re-usuable view controller.
The configuration looks something like the following:
Thus you can use the same embedded VC for different tabbar item. Obviously if you need the reference of the tabbarcontroller, you need to use self.parentViewController.tabBarController instead of self.tabBarController directly. But it solves the issue of reusing a VC right from the storyboard.
I've found much simpler solution using storyboard only.
Setup your storyboard like this:
Then in your Navigation Controller Identity Inspector set Restoration ID like this:
And in your ViewController class file put the following code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = parent?.restorationIdentifier
label.text = parent?.restorationIdentifier
}
or do what you like based on parent?.restorationIdentifier value
If you don't want the Navigation TopBar to appear on the ViewController just set it to None in Attributes Inspector of the desired Navigation Controller like this:
That's it! Hope it helps.
Yes you can.
All you need to do is to create a new View Controller in StoryBoard as if there is going to be a different View Controller for tab 2. Then Select the 2nd view controller and simply add its class name the same classname of view controller 1
Things to note:
When you are sharing the same view controller class (.m ad .h) files, each tab will create a NEW instance of that class.
Edit:
This works as long as you have either a "custom" cell scenario (i.e. reusing two table view controllers) OR, have all your views inside a "container view" (i.e. reusing UIView).
I needed slightly different solution than the accepted answer. I needed to use same Table View Controller with the different data source for different tab bar items. So in the storyboard, i created two Navigation Controllers with same classes like this;
I also give different "Restoration ID" to each of them.
For the first one, I gave "navCont1" and "navCont2" for the second one.
In subclass("GeneralNavCont") of these Navigation Controllers; I override init method and check restoration id of self. Then i initiate my TableViewController and set its data source based on ids like this;
class GeneralNavCont: UINavigationController {
var dataSource1 = [Countries]()
var dataSource2 = [Cities]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initiateTableVCBasedOnId()
}
func initiateTableVCBasedOnId() {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tableVC = storyBoard.instantiateViewController(withIdentifier: "tableVC") as! MyTableViewController
if self.restorationIdentifier == "navCont1" {
tableVC.dataSource = self.dataSource1
self.viewControllers = [tableVC]
}
else if self.restorationIdentifier == "navCont2" {
tableVC.dataSource = self.dataSource2
self.viewControllers = [tableVC]
}
}
}
Hope it helps someone. Cheers.