2

enter image description hereI'm having trouble displaying different navigation bar button on different views.

I have a parent view navigation controller and 3 child views. What I would like to do is have different navigation bar buttons on each child view, and not just the same 2 which I could achieve through adding them on the main storyboard.

I have attached two screenshots so you can see what my storyboard and code looks like.

So basically I'm looking for some code to add bar items individually on each view. [![enter image description here][2]][2] enter image description here

1 Answers1

5

In each view controller you can use self.navigationItem to make those changes individually.

For example, say you have a view controller names "VC1" and you want to have an add button in the navigation bar. In VC1, override viewDidLoad and do the following:

override func viewDidLoad() {
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addFunc")
}

UPDATE: So, looking at your tutorial I realized that you're not pushing the child view controllers to the navigation controller, you're just adding them as child. There is a difference. Every navigation controller has a set of view controllers (you can access this: navController.viewControllers)

You can add child view controllers but it would not be any different from other controllers, if you want to actually use a navigation controller, you need to push them to the navigation controller. Otherwise, you can't access navigationItem or similar features like that.

Instead of adding the view controllers to the scroll view, use this:

self.pushViewController(childViewController1, animated: true)

If you absolutely want to have it in the scroll view AND have different navigation bar buttons for every view controller, you'd have to implement this mechanism yourself. For example, check out this:

https://github.com/peymanmortazavi/UISwipeViewController

It's not polished, you'd have to implement the layout constraints properly but it demonstrates what I mean.

Peyman
  • 3,059
  • 1
  • 33
  • 68
  • Ok, Thanks for all your help you'll benefit from it one day ;) –  Nov 23 '15 at 20:33
  • @Charles added the code in the answer. Let me know if that's what you're looking for. – Peyman Nov 23 '15 at 22:57
  • that's brilliant, absolutely perfect!! I couldn't thank you enought its been driving me mad! –  Nov 23 '15 at 23:19
  • I shall have several well plenty of views coming of those pages, is the best way to add all these features through code or can I connect VC to the code you wrote and add and drag most of the actions I need? I heard somewhere that the more code there is, the more likely apple will reject it?! not sure if thats true though –  Nov 23 '15 at 23:20
  • I don't quite understand what you mean by that. You can just add it to your project and use it. Apple wouldn't care about the size of your source code. don't worry about it. Have as many view controllers as your want. – Peyman Nov 23 '15 at 23:33
  • I know, I just meant once its in my project all the views I would like to add to those views I shall have to do it by code? and I was wondering if its possible to do it via storyboard. But it doesnt matter I will figure all the code it! one last question and I'll let you go!! how do I add the bouncing vertical aswell? so I can scroll vertically? –  Nov 23 '15 at 23:37
  • and where could I use this code to make the second view appear at launch? self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 1) –  Nov 24 '15 at 00:02
  • No my implementation isn't using scroll view at all. You can modify the code to support that, it doesn't have it right now. I'd suggest implementing vertical scrolling inside the child view controllers – Peyman Nov 24 '15 at 00:32
  • Hi mate, I have a couple of questions I cant seem to figure out! if you dont mind, shall I create new questions and you could see if you can answer them? –  Nov 24 '15 at 14:49
  • Sorry to bother you, but I was wondering if the code /example you did for me, which is great, if its possible to achieve the same thing but with 3 viewcontrolelr on the main.storyboard to edit? as of my lackof experience at the moment it probably would be alot easier for myself! all though I'm trying to figure it out, I don't think I shall be able to add all the features i would like in my app i.e. images through code! –  Nov 24 '15 at 18:54
  • @Charles You can send me the question links and I'd be happy to answer them. As far as the Storyboard support goes, I'll update the code in a few hours. Check in like 4 hours to see the changes. – Peyman Nov 24 '15 at 19:01
  • you don't realise how much I appreciate your help! –  Nov 24 '15 at 19:13
  • I'll wait for the code, and hopefully i'll be able to fix my questions my self. –  Nov 24 '15 at 19:14
  • I can't do that in storyboard, but you can create your view controllers in storyboard and get them in code: http://stackoverflow.com/questions/24035984/instantiate-and-present-a-viewcontroller-in-swift – Peyman Nov 24 '15 at 22:20
  • no worries, please may you check this question out http://stackoverflow.com/questions/33905201/navbar-issues-swift –  Nov 24 '15 at 22:40