My goal is to add a cancel bar item on a navigation bar. The scenario is a user presses the button then it will segue modally to another UIView Controller and there will be a cancel button. The cancel button will bring the use back to the first screen
Example
what I did currently is drag a navigation bar onto UIViewController, it works but when I try to drag bar item onto the navigation bar, it doesn't work. What am I missing right now?
Add UIBarButtonItem in navigation bar programmatically
let btnCancel = UIButton()
btnCancel.setImage(UIImage(named: "crossbuttonimagename"), forState: .Normal)
btnCancel.frame = CGRectMake(0, 0, 25, 25)
btnCancel.addTarget(self, action: Selector("youraction"), forControlEvents: .TouchUpInside)
//Set Left Bar Button item
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnCancel
self.navigationItem.leftBarButtonItem = leftBarButton
Embed your Second View controller into a Navigation Controller.
Present modally this Navigation Controller.
Drag & Drop an UIBarButton Item (X button) to the Navigation Bar in the Second View Controller.
Create an action to go back to First View Controller.
#IBAction func actionDismiss(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Assign this action to the Cancel Button.
Select the segue which leads to the problematic view controller
Set that to PUSH segue
Again unselect the PUSH segue and now accepted SHOW segue
After these step you can add right bar button item using storyboard.
Related
I've embed in the navigation bar in app, everything work good, except when entering a view where I have set up the navigation bar programmatically (segue to settings, reset function).
It shows just the custom navigation bar, which is ok, but if I implement a custom back button, the whole app has the same navigation bar as problematic one (now it shows the reset and settings button everywhere).
Is there a way to make the navigation bar custom only to that specific view?
Part of the code:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
//Add gesture to MainLabel
let tapLabel: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(resetTime))
tapLabel.delegate = self
mainLabel.isUserInteractionEnabled = true
mainLabel.addGestureRecognizer(tapLabel)
//Add gesture to UINavigationBar title
let tapTitle: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(resetTime))
tapTitle.delegate = self
self.navigationItem.titleView = resetLabel
self.navigationItem.titleView?.isUserInteractionEnabled = true
self.navigationItem.titleView?.addGestureRecognizer(tapTitle)
}
yes, you can hide navigation button on viewDidDisappear of a viewController in which you want custom navigation bar and in viewDidAppear unhide buttons which you need.
I have a navigation controller so when I click on a button on my main view, it opens another view with the "Back" navigation item.
What I'm trying to do is to add another item on my second view on the right, so this view should have a "Back" item, already working, and a new "Edit" item which does the same thing as Back button but I can get the prepareForSegue function to run some code in complement.
How can I achieve to do that ?
Here is what I tried :
I'm using two navigation controllers because I couldn't add an "Edit" item if my second view wasn't embed in a navigation controller.
So this is working, I have my two "Back" and "Edit" items, but I don't know how to manage to get the same behavior as the "Back" item on my "Edit" item, I tried segues but it doesn't work as expected.
Note that the button I talked about on my first view isn't shown in the editor nor its segue, I did it programmatically.
You can simple call popViewController(animated:) when the edit button is tapped.
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let editItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = editItem
}
#objc func editButtonTapped(_ sender: UIBarButtonItem)
{
self.navigationController?.popViewController(animated: true)
}
}
Storyboard:
I have the next structure in swift 3:
Navigation Controller -> Tab Bar Controller -> 4 View Controllers
I have 3 commons bar buttons items.
My problem is, in just one of the view Controllers I want to add new bar button item on the top-right corner. This way when I navigate to the others views the new button should not appear.
How can I solve this?
Thanks!
You should set it with tabBarController like this in Swift:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "imageName"), for: .normal)
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
let leftButtonBar = UIBarButtonItem(customView: button)
tabBarController?.navigationItem.rightBarButtonItems = [leftButtonBar].
this is best answer forever and works correctly.
You can have the below structure by which you can have individual
navigation to add button for each view controller
Tab Bar Controller ->Navigation Controller -> 1st View Controllers
->Navigation Controller -> 2nd View Controllers
->Navigation Controller -> 3rd View Controllers
->Navigation Controller -> 4th View Controllers
I did search something similar with my question, but didn't find anything to help me solve my issue.
Is it possible to set behavior for navigation bar item as tab bar item?
I use TabBarController. Each tab item should show the corresponding ViewController. When user tap on the item in nav bar it should show the corresponding ViewController too with the bottom tab bar. And this ViewController should be as tab bar controller, but not display tab item in the bottom tab bar.
How I can achieve the scenario describe above? Or any alternative idea how it possible to implement, please! Almost always the top and bottom bars should be visible for each ViewController.
Drag and drop from your bar button item in IB to your class file and define an outlet, then in viewDidLoad create a gesture recognizer, add an action to your gesture recognizer and you are all set. I hope it helps.
class SomeClass: UIViewControler {
let someBarButton:UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(SomeClass.tapFunction))
someBarButton.addGestureRecognizer(tap)
}
func tapFunction() {
//take me to a view controller
self.performSegue(withIdentifier: "myIdentifier", sender: self)
}
}
I want to make an app in which my navigation bar appears on all pages. When I click on any row of side menu(done through SWRevealViewController), I want the page that opens to have a navigation bar on top. In the image below, I want the same navigation bar as the HomeViewController on the page that has "Menu" label. How can I do this? Please help. I am new to iOS. I am doing this in Xcode 8 and Swift 3.
EDIT : I want something like this: I have placed the side menu button on the reveal view controller. I can see it on the front view controller at runtime but how to connect the target and action of revealviewcontroller then, so that the side menu opens? If this is done then my problem of navigation controller on "Menu" label page will be solved automatically
Suppose outlet of menu is btn_Menu. In controller's view did load I set bar button's action and target programmatically.
btn_Menu.target = self.revealViewController()
btn_Menu.action = #selector(SWRevealViewController.revealToggle(_:))
Have a look at this image structure that you need. Sorry not very clear but perhaps solve your issue:
You need to add all the starting points of your ViewControllers in a NavigationController of their own so in this case you need to embed the ViewController which has the menu label in a NavigationController and make the side menu segue to the NavigationController not the ViewController
hope my answer helps you
1) create a new ViewController Class called 'BaseViewController'
2) in the BaseViewController viewDidLoad add the following code and change the images names and targets also add the left button for the menu
let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)
3) make the viewControllers which you will show from the side menu like the one with menu label inherits from BaseViewController and in it's viewDidLoad make it call super.viewDidLoad()
4) embed the viewControllers which you will show from the side menu in a navigationController
Using Container View may help.
Replace the UIView in the middle (Content) with a Container View and that container can be a UINavigationController. And to avoid having 2 NavigationBars you can hide the NavigationBar of the ContainerView.
The blue highlighted view is the ContainerView inside the Controller that is implementing SideMenu
You also need to add Navigation Controller for sabe bar Controller
Follow the steps :
Select side bar viewcontroller > go to Editior menu > Embed in > Click Navigation Controller