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.
Related
I have an app where there is a navigation controller which can have navigation child. I have to put a UIButton where the navigation bar is normally.
The UIButton is connected to targets:
for button in arrayOfButtons {
button.addTarget(self, action: #selector(buttonTouchUpInsideP3), for: [.touchUpInside])
button.addTarget(self, action: #selector(buttonTouchInsideBoundsP3), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(buttonDraggedOutOfBoundsP3), for: [.touchDragExit, .touchCancel]) //there is definitly combining that cud be done here...
}
And basically I can't simply hide the navigationBar as I use it for sizing/autolayout, so what I try to do is: (this code is in the ViewDidLoad of the child navigation controller)
self.navigationController?.navigationBar.isUserInteractionEnabled = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationItem.hidesBackButton = true
To remove its UI and have it invisible up there, this part works... however, the issue is that I still cannot press anything in the navigationBar area... even though I do isUserInteractionEnabled = false.
How can I fix this!? So that I can tap a UIButton underneath(?) or rather where the navigationBar is located??
Haven't seen a SO question that has solved this for me :/
Well, for anyone that wants to know:
#StonedStudio was right, I ended up just hiding the navigation bar and instead I simply created a view with the bounds height of the navigationBar anchored to the top of the view.safeAreaLayoutGuide.topAnchor and that way it takes the shape of the would be navigation bar while allowing the tap as well! :)
Essentially I have a UIButton created in StoryBoards that I would like to drag into the navigation bar. This UIButton has an action associated with it when touched but all touch events stop working when I drag this UIButton into the navigation bar.
Is there additional code I need to the IBAction item when doing this?
One of the main reasons I want to add a UIButton instead of a BarButtonItem to the navigation bar is simply because it allows me to add a background color and curve the edges of the button.
I don't think you can do it in the storyboard. You have to do it in code, using the init(customView:) initialiser.
let myCustomButtonWithBackgroundsAndStuff = UIButton(type: .custom)
...
// this is the equivalent of connecting the IBAction, in code form, in case you didn't know
myCustomButtonWithBackgroundsAndStuff.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: myCustomButton)
navigationItem.rightBarButtonItem = barButtonItem
// or append to rightBarButtonItems if you have other bar button items
...
#objc func buttonAction() {
...
}
I have a ViewController (LandingViewController) and one drawerViewController.
I am using SWRevealViewController and I added a gesture to Landing view but when it will open the DrawerViewController then it is covering status bar only once. I don't know why this is happening, But it is hiding once and after that working fine.
I am not using NavigationBar and I have a map view in my LandingViewController.
I have drawer button in my LandingViewController.
If I will open or reveal the view by button it's working fine. But if I will open it by view it is hiding status bar.
Here below is my code :-
btnDrawerMenu.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: UIControlEvents.touchUpInside).
//Reveal View Drawer menu
fileprivate func createReveal() {
if self.revealViewController() != nil {
self.revealViewController().rearViewRevealWidth = self.view.frame.width - 20
self.revealViewController().rearViewRevealOverdraw = 0.0
self.revealViewController().bounceBackOnOverdraw = false
self.revealViewController().bounceBackOnLeftOverdraw = false
self.revealViewController().toggleAnimationType = .easeOut
}
}
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.
How do I set a title and buttons on the navigation bar of a pushed view controller if I push it onto navigationController with navigationController.pushViewController(controller:animated:completion:), whilst keeping the "Back" button?
Thank you in advance!
For anyone wondering how to do this, just override your pushed UIViewController's navigationItem property directly. My mistake was that I was trying to accomplish this with self.navigationController?.navigationItem
Here is the code, that you should write in order to set the navigation title as well as left button
Code :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//For Setting Title
self.title = "New Title";
//For setting button in place of back button
let leftItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "leftButtonClicked");
self.navigationItem.leftBarButtonItem = leftItem;
}
Happy coding ...