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.
Related
I want to achieve something similar to Facebook's way of presenting the comments view controller for a specific post. In below picture one can see, that the pushed new view controller is presented "full screen" (for the lack of a better way of describing the behaviour). It seems to me like some kind of modal segue rather than a push one. When trying to recreate that in my own app I can't achieve that the whole navigation bar is included in the presentation segue. Only the view inside the presentation hierarchy is changed. I want the second view controller to be entirely white (the view as well as the navigation bar) but both view controllers should have the default swipe-to-go-back behaviour. How can that be done?
What they're probably doing is hiding the navigation bar.
You can achieve the same effect if you set navigationController?.navigationBar.isHidden = true (can also do it on navigation bar in UINavigationController from the storyboard), make a regular show segue and just display it using performSegue(withIdentifier: "nextScreen", sender: nil). You can then make your own UI logic for displaying back buttons etc.
i am wanting to add a barButtonItem onto a tabBar so i don't have to use a navigation controller at the top of the screen.
My ViewControllers are embedded in a tabBarController:
And i want to add a barButton on each viewcontroller e.g.:
and them implement some code like:
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 4 {
}
}
to run a function when this button is tapped.
problem is this button isn't showing on the tabBar when the app is built.
i was hoping this would achievable without having to create a custom tabBar.
I don't quite understand what the problem you're describing is. Are you trying to add a 'Logout' button at the top right of your screen by only using a UITabBarController? Why are you against using a UINavigationController? It's incredibly easy to implement using storyboards (which it looks like you're using anyway).
Apple even has a page on how to implement a UINavigationController within a UITabBarController here: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html
Key information here:
To create a combined interface with three tabs that contain custom view controllers and one tab that contains a navigation controller:
Create three custom view controllers, one for each tab, and a navigation controller.
Select the three custom view controllers and the navigation controller (only the navigation controller scene, not it’s root view
controller).
Choose Editor > Embed In > Tab Bar Controller.
Display the tab bar controller as the first view controller by selecting the option Is Initial View Controller in the Attributes
inspector (or present the view controller in your user interface in
another way.)
According to Apple Documentation,
UIBarButtonItem - A bar button item is a button specialized for
placement on a UIToolbar or UINavigationBar object. It inherits basic
button behavior from its abstract superclass, UIBarItem. The
UIBarButtonItem defines additional initialization methods and
properties for use on toolbars and navigation bars.
Since you're against putting a Navigation Controller in your project, a UINavigationBar is not implemented which is what UIBarButtonItems are for. If you want to implement your Menu Button on top of your screen in one of your view controllers. Why not just use a UIButton as inside a UIView like this?? The blue background is just a regular UIView with UIButtons inside of it.
My requirement is I have to implement two SWRevelViewController. I have to show two right view for a single view controller. I mean I have to use RevealView for two View controllers,But it is showing error if I am doing through storyboard.
Can I do by programmatically? Or any alternative?
Going through your comment "I have a View controller ok, I have to show left view reveal. I have one more view controller and for that i want to show right reveal. So for two different view controllers how can i get it"
You can Add Two reveal controller's home but with Different Storyboard Ids
As screenshot provided you can set the class name for that second view controller on which you are willing to have reveal with right menu, with different storyboard id.
Now we have to set rear and front controllers for swreveal controller
Steps to set Swreveaal front and real view controller
Add two View controllers for rear and front view
Now drag seques and set class to the segue SWRevealViewControllerSegueSetController.
now as in screen shot below set "sw_rear" for rear view controller and set "sw_front" for front view controller.
for setting right view controller
Now add right button on your target controller from where right menu is needed
You can invoke right menu as
if self.revealViewController() != nil {
toggleMenuBtn.addTarget( self.revealViewController(), action:#selector(SWRevealViewController.rightRevealToggle(_:)) , forControlEvents: UIControlEvents.TouchUpInside)
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
You can do it using storyboard too. You can do it by making custom segue with the front and back VC. And Assign custom segue on selection of backTableVC row.
This link will helps you:
http://www.appcoda.com/sidebar-menu-swift/
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?).
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