Labels on navigationbar in swift












1















I have three views in navigationBar.



I want to place labels on navigation bar so that when i will go to one view to another it will show the page number . Here is my code. I have written this code in The viewDidLoad of all the three class



     if let navigationBar = self.navigationController?.navigationBar {

let firstFrame = CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10)

let secondFrame = CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10)

let thirdFrame = CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10)

let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "1"

let secondLabel = UILabel(frame: secondFrame)
secondLabel.text = "of"
let thirdLabel = UILabel(frame: thirdFrame)
secondLabel.text = "3"

navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
}


But the problem is the same label repeating in every view controller.It is showing like "1 of 3" in every view controller *










share|improve this question

























  • What are you using? UIPageViewController?

    – Robert Dresler
    Nov 23 '18 at 19:55











  • No , ViewController with Navigation controller

    – Tapan
    Nov 23 '18 at 19:56











  • And you just go from first ViewController to second and from second to third?

    – Robert Dresler
    Nov 23 '18 at 19:59











  • yes . first to second and second to third

    – Tapan
    Nov 23 '18 at 20:00
















1















I have three views in navigationBar.



I want to place labels on navigation bar so that when i will go to one view to another it will show the page number . Here is my code. I have written this code in The viewDidLoad of all the three class



     if let navigationBar = self.navigationController?.navigationBar {

let firstFrame = CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10)

let secondFrame = CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10)

let thirdFrame = CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10)

let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "1"

let secondLabel = UILabel(frame: secondFrame)
secondLabel.text = "of"
let thirdLabel = UILabel(frame: thirdFrame)
secondLabel.text = "3"

navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
}


But the problem is the same label repeating in every view controller.It is showing like "1 of 3" in every view controller *










share|improve this question

























  • What are you using? UIPageViewController?

    – Robert Dresler
    Nov 23 '18 at 19:55











  • No , ViewController with Navigation controller

    – Tapan
    Nov 23 '18 at 19:56











  • And you just go from first ViewController to second and from second to third?

    – Robert Dresler
    Nov 23 '18 at 19:59











  • yes . first to second and second to third

    – Tapan
    Nov 23 '18 at 20:00














1












1








1








I have three views in navigationBar.



I want to place labels on navigation bar so that when i will go to one view to another it will show the page number . Here is my code. I have written this code in The viewDidLoad of all the three class



     if let navigationBar = self.navigationController?.navigationBar {

let firstFrame = CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10)

let secondFrame = CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10)

let thirdFrame = CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10)

let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "1"

let secondLabel = UILabel(frame: secondFrame)
secondLabel.text = "of"
let thirdLabel = UILabel(frame: thirdFrame)
secondLabel.text = "3"

navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
}


But the problem is the same label repeating in every view controller.It is showing like "1 of 3" in every view controller *










share|improve this question
















I have three views in navigationBar.



I want to place labels on navigation bar so that when i will go to one view to another it will show the page number . Here is my code. I have written this code in The viewDidLoad of all the three class



     if let navigationBar = self.navigationController?.navigationBar {

let firstFrame = CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10)

let secondFrame = CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10)

let thirdFrame = CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10)

let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "1"

let secondLabel = UILabel(frame: secondFrame)
secondLabel.text = "of"
let thirdLabel = UILabel(frame: thirdFrame)
secondLabel.text = "3"

navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
}


But the problem is the same label repeating in every view controller.It is showing like "1 of 3" in every view controller *







ios swift controller navigation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:15









E.Coms

2,0522414




2,0522414










asked Nov 23 '18 at 19:35









TapanTapan

55




55













  • What are you using? UIPageViewController?

    – Robert Dresler
    Nov 23 '18 at 19:55











  • No , ViewController with Navigation controller

    – Tapan
    Nov 23 '18 at 19:56











  • And you just go from first ViewController to second and from second to third?

    – Robert Dresler
    Nov 23 '18 at 19:59











  • yes . first to second and second to third

    – Tapan
    Nov 23 '18 at 20:00



















  • What are you using? UIPageViewController?

    – Robert Dresler
    Nov 23 '18 at 19:55











  • No , ViewController with Navigation controller

    – Tapan
    Nov 23 '18 at 19:56











  • And you just go from first ViewController to second and from second to third?

    – Robert Dresler
    Nov 23 '18 at 19:59











  • yes . first to second and second to third

    – Tapan
    Nov 23 '18 at 20:00

















What are you using? UIPageViewController?

– Robert Dresler
Nov 23 '18 at 19:55





What are you using? UIPageViewController?

– Robert Dresler
Nov 23 '18 at 19:55













No , ViewController with Navigation controller

– Tapan
Nov 23 '18 at 19:56





No , ViewController with Navigation controller

– Tapan
Nov 23 '18 at 19:56













And you just go from first ViewController to second and from second to third?

– Robert Dresler
Nov 23 '18 at 19:59





And you just go from first ViewController to second and from second to third?

– Robert Dresler
Nov 23 '18 at 19:59













yes . first to second and second to third

– Tapan
Nov 23 '18 at 20:00





yes . first to second and second to third

– Tapan
Nov 23 '18 at 20:00












1 Answer
1






active

oldest

votes


















0














First create subclass of UINavigationController



class NavigationController: UINavigationController {
}


don't forget to change class of your UINavigationController in Storyboard



enter image description here



now in your UINavigationController subclass create properties of your labels and set them



class NavigationController: UINavigationController {

var firstLabel: UILabel?
var secondLabel: UILabel?
var thirdLabel: UILabel?

override func viewDidLoad() {
super.viewDidLoad()
firstLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10))
secondLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10))
thirdLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10))
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
firstLabel.text = "1"
secondLabel.text = "of"
thirdLabel.text = "3"
}
}


then you want to change firstLabel.text every time you move to another ViewController. So, in ViewController in viewWillAppear get reference for your NavigationController and set text of this firstLabel like this



class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "1"
}
}
}


and then do the same for second and third UIViewController



class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "2"
}
}
}

...





share|improve this answer


























  • Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

    – Tapan
    Nov 23 '18 at 21:03











  • @Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

    – Robert Dresler
    Nov 23 '18 at 21:05













  • Thank you. Thanks a lot Robert. It is working now

    – Tapan
    Nov 23 '18 at 21:16











  • @Tapan No problem, I'm glad it works!

    – Robert Dresler
    Nov 23 '18 at 21:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452136%2flabels-on-navigationbar-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First create subclass of UINavigationController



class NavigationController: UINavigationController {
}


don't forget to change class of your UINavigationController in Storyboard



enter image description here



now in your UINavigationController subclass create properties of your labels and set them



class NavigationController: UINavigationController {

var firstLabel: UILabel?
var secondLabel: UILabel?
var thirdLabel: UILabel?

override func viewDidLoad() {
super.viewDidLoad()
firstLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10))
secondLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10))
thirdLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10))
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
firstLabel.text = "1"
secondLabel.text = "of"
thirdLabel.text = "3"
}
}


then you want to change firstLabel.text every time you move to another ViewController. So, in ViewController in viewWillAppear get reference for your NavigationController and set text of this firstLabel like this



class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "1"
}
}
}


and then do the same for second and third UIViewController



class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "2"
}
}
}

...





share|improve this answer


























  • Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

    – Tapan
    Nov 23 '18 at 21:03











  • @Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

    – Robert Dresler
    Nov 23 '18 at 21:05













  • Thank you. Thanks a lot Robert. It is working now

    – Tapan
    Nov 23 '18 at 21:16











  • @Tapan No problem, I'm glad it works!

    – Robert Dresler
    Nov 23 '18 at 21:18
















0














First create subclass of UINavigationController



class NavigationController: UINavigationController {
}


don't forget to change class of your UINavigationController in Storyboard



enter image description here



now in your UINavigationController subclass create properties of your labels and set them



class NavigationController: UINavigationController {

var firstLabel: UILabel?
var secondLabel: UILabel?
var thirdLabel: UILabel?

override func viewDidLoad() {
super.viewDidLoad()
firstLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10))
secondLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10))
thirdLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10))
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
firstLabel.text = "1"
secondLabel.text = "of"
thirdLabel.text = "3"
}
}


then you want to change firstLabel.text every time you move to another ViewController. So, in ViewController in viewWillAppear get reference for your NavigationController and set text of this firstLabel like this



class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "1"
}
}
}


and then do the same for second and third UIViewController



class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "2"
}
}
}

...





share|improve this answer


























  • Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

    – Tapan
    Nov 23 '18 at 21:03











  • @Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

    – Robert Dresler
    Nov 23 '18 at 21:05













  • Thank you. Thanks a lot Robert. It is working now

    – Tapan
    Nov 23 '18 at 21:16











  • @Tapan No problem, I'm glad it works!

    – Robert Dresler
    Nov 23 '18 at 21:18














0












0








0







First create subclass of UINavigationController



class NavigationController: UINavigationController {
}


don't forget to change class of your UINavigationController in Storyboard



enter image description here



now in your UINavigationController subclass create properties of your labels and set them



class NavigationController: UINavigationController {

var firstLabel: UILabel?
var secondLabel: UILabel?
var thirdLabel: UILabel?

override func viewDidLoad() {
super.viewDidLoad()
firstLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10))
secondLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10))
thirdLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10))
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
firstLabel.text = "1"
secondLabel.text = "of"
thirdLabel.text = "3"
}
}


then you want to change firstLabel.text every time you move to another ViewController. So, in ViewController in viewWillAppear get reference for your NavigationController and set text of this firstLabel like this



class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "1"
}
}
}


and then do the same for second and third UIViewController



class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "2"
}
}
}

...





share|improve this answer















First create subclass of UINavigationController



class NavigationController: UINavigationController {
}


don't forget to change class of your UINavigationController in Storyboard



enter image description here



now in your UINavigationController subclass create properties of your labels and set them



class NavigationController: UINavigationController {

var firstLabel: UILabel?
var secondLabel: UILabel?
var thirdLabel: UILabel?

override func viewDidLoad() {
super.viewDidLoad()
firstLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10))
secondLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10))
thirdLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10))
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
firstLabel.text = "1"
secondLabel.text = "of"
thirdLabel.text = "3"
}
}


then you want to change firstLabel.text every time you move to another ViewController. So, in ViewController in viewWillAppear get reference for your NavigationController and set text of this firstLabel like this



class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "1"
}
}
}


and then do the same for second and third UIViewController



class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "2"
}
}
}

...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 21:19

























answered Nov 23 '18 at 20:08









Robert DreslerRobert Dresler

5,5781526




5,5781526













  • Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

    – Tapan
    Nov 23 '18 at 21:03











  • @Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

    – Robert Dresler
    Nov 23 '18 at 21:05













  • Thank you. Thanks a lot Robert. It is working now

    – Tapan
    Nov 23 '18 at 21:16











  • @Tapan No problem, I'm glad it works!

    – Robert Dresler
    Nov 23 '18 at 21:18



















  • Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

    – Tapan
    Nov 23 '18 at 21:03











  • @Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

    – Robert Dresler
    Nov 23 '18 at 21:05













  • Thank you. Thanks a lot Robert. It is working now

    – Tapan
    Nov 23 '18 at 21:16











  • @Tapan No problem, I'm glad it works!

    – Robert Dresler
    Nov 23 '18 at 21:18

















Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

– Tapan
Nov 23 '18 at 21:03





Hi Robert, I have done as you suggest. but why the label is not showing. Am i doing any thing wrong ?

– Tapan
Nov 23 '18 at 21:03













@Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

– Robert Dresler
Nov 23 '18 at 21:05







@Tapan I'am sorry, I have just updated my answer. In NavigationController subclass in viewDidLoad I forgot to add subviews to navigationBar

– Robert Dresler
Nov 23 '18 at 21:05















Thank you. Thanks a lot Robert. It is working now

– Tapan
Nov 23 '18 at 21:16





Thank you. Thanks a lot Robert. It is working now

– Tapan
Nov 23 '18 at 21:16













@Tapan No problem, I'm glad it works!

– Robert Dresler
Nov 23 '18 at 21:18





@Tapan No problem, I'm glad it works!

– Robert Dresler
Nov 23 '18 at 21:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452136%2flabels-on-navigationbar-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...