I have this situation :
in my app I have a page view controller within view controllers and navigation controller. I want add right item in navigation bar when appear a particular view controller in page view controller and for this reason I did this in the view controller where I want to add right button:
override func viewDidAppear(animated: Bool) {
let camera:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("test"))
self.navigationItem.rightBarButtonItem = camera
}
but it doesn't work , I don't see the right button.
What do I wrong?
EDIT ONE:
this is my situation in storyboard:
The problem is that self.navigationItem is used only when this view controller is the direct child of a navigation controller. That's not the situation here. The direct child of the navigation controller is some other view controller — maybe the page view controller, or something that contains the page view controller (hard to tell from your description). You need to rethink your approach here. You can do what you want to do, but only by somehow sending a signal to the direct child of the navigation controller and having him configure his navigation item.
So, for your configuration, the only view controller whose navigationItem can affect the navigation controller automatically is the one with the table view (the navigation controller's direct child) or whoever is pushed on top of it (the one with the pager view controller?).
Related
My second view controller is a table view controller. I wish to segue to a first view controller with a back button. I have tried using Editor>Embed In>Navigation Controller. It places a navigation bar on the top where I can place an Item (Back button) on it, and after segueing it back to my first view controller, it becomes very screwy (it shows two navigation bars on top of each other). Is there a better/easier way to move back to my first view controller?
if u want to use navigation controller to manage ur push&pop stuff, just make sure:
navigation controller is ur initial view controller
ur first view controller is the root view controller of navigation controller
set an unique identifier to second view controller
leave ur second view controller alone without any link to other vc
like this
then push like this
let secondVC = (self.storyboard?.instantiateViewControllerWithIdentifier("identifier"))! as! urVC
secondVC.title = "second view controller"
self.navigationController?.pushViewController(secondVC, animated: true)
and pop like this
self.navigationController?.popViewControllerAnimated(true)
alternatively, u can use unwind segue following this tutorial
I am interested in implementing a back button to segue to a previous view controller. I have tried to embed a navigation bar onto the the top of the table view controller but the issue is when I do segue back to the first view controller, by default, Xcode keeps the navigation bar with a back button to go back to the table view controller. Is there a simpler way to implement a segue to go back to the first VC without the navigation bar remaining?
I'm not too sure if this works, but embed your view controllers including the first one inside the navigation controller. That would make all your view controllers with navigation bar above.
On the main view controller (the one you do not want to have the navigation bar), add the line of code inside your viewDidLoad method.
Swift 3:
self.navigationController?.navigationBar.isHidden = true
I found an easy way. On your TableViewController, drag a UIview to the top of the view controller and itll let you insert the view. From there just add your back button
Just assign your "Back" UIBarButtonItem to this method and it should work.
#IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Sounds like a few problems
1) How are you navigating from the first view controller? Is it with Show (Push) if not, you are replacing the first view controller in your stack.
2) Based on your problem, make sure your first view controller is not embedded in a navigation controller
3) As Dylan mentioned, you need to hook your back button to remove the current view controller to return to the first one
4) Extension of (3), how are you segueing back to the first view controller? If you are explicitly navigating to first view controller with your back button handler, it's not actually going back but rather forward and keep the navigation bar.
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]
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.
How do you set the right bar button from the navigation controller instead of the view controller?
I've been reading about writing SOLID view controller which work only as controllers instead of adding everything to them.
The post suggested to move the navigation logic outside the view controller (which I agree with). Also, the view controller really shouldn't be setting up the navigation controller it's embedded into, so I'm trying to move the code which sets up the navigation bar to my custom UINavigationController class.
I was partly successful, I managed to update the color, but wasn't able to set a rightBarButtonItem.
Adding these lines to the a view controller will work, but I want to do it from the UINavigationController subclass.
let settingsImage = UIImage(named: "settingsButton")
self.rightBarButton = UIBarButtonItem(image: settingsImage, style: UIBarButtonItemStyle.Plain, target: self, action: "showSettings")
self.navigationItem.rightBarButtonItem = self.rightBarButton;
I've looked at the questions "Default navigation button" which suggest creating a parent view controller and subclassing all view controllers from that, but I really don't like the fact that the view controller is setting up the navigation controller. It should be the other way around.
Edit: I got as far as self.navigationBar.items. If I set that to nil, nothing will be shown. So I probably need to change that. Not yet sure how though.
The navigation controller doesn't own the navigation bars that are displayed and so doesn't own the buttons on them.
The navigation controller pushes view controllers to the screen and gives them a navigation bar. The navigation bar is then configured (title, bar buttons, etc...) by the view controller. With the exception of the back button which is given a default configuration of the title of the previous controller.
So anyway, this is not "setting up the navigation controller" this is configuring the navigation bar that belongs to the view controller.
Best place for this is the view controller itself.
If you'd like a default button across the entire app then you could create a "parent subclass" for all your view controllers. (But then you're limited to the type of controller you can use).
Or I have done this in the past by creating a category on UIBarButtonItem with a method like this...
+ (UIBarButtonItem *)myDefaultButtonWithTarget:(id)target action:(selector)action
It means that you have to call it in each view controller you want ...
self.navigationItem.rightBarButtonItem = [UIBarButtonItem myDefaultButtonWithTarget:self action:#selector(doSomething)];
But it means that you only need one line of code to do it instead of having to configure it each time. Also, you can change the button everywhere by changing the category.
Documentation:
https://developer.apple.com/reference/uikit/uinavigationbar
I have app with UICollectionViews in UIPageViewController based on this tutorial for UIPageViewController, now I've embedded main view controller in navigation controller, and I want to change title of nav bar dynamically when in detail view, but how can I access it?
At first I've tried to embed in navigation controller Page View Controller, but nav bar wasn't displayed.
edit:
I've found out, that I can add navigation item to Time Snap Detail View Controller and set its title here, even though it's not visible in storyboard.
But how can I edit title in navigation bar, when I'm in Page View Controller?(In this case, on stack of the navigation controller is Main View Controller, which has Page View Controller as child view controller).
I tried this, and it's ok:
childVC.parentViewController.navigationItem.title = #"abc";
The parent in view controller is not working anymore. Looks like the only way is to save the parent in this child view controller's method and set it's title:
override func didMove(toParent: UIViewController?)
try this. it's working for me in almost same scenario
self.navigationController.parentViewController.navigationItem.title = #"sdfsdf.";