How would I hide the navigation bar once my imageView is tapped, the navigation bar messes up the view of the full screen image once my imageView is tapped and I would like it hidden when the image is tapped and to reappear once the image is dismissed. Here is my code for my image being tapped.
//expandImage
#IBAction func expand(_ sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreen))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreen(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Add this to your expand() method:
self.navigationController?.setNavigationBarHidden(true, animated: true)
And in dismissFullscreen() method:
self.navigationController?.setNavigationBarHidden(false, animated: true)
Or you can create new ViewController, pass image to it (with segue e.g) and add this to viewDidLoad() of new ViewController:
self.navigationController?.hidesBarsOnTap = true
So here is how you can do that:
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let tapImageScrollView = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapImageScrollView)
}
func imageTapped(_ sender: UIGestureRecognizer) {
if self.navigationController?.navigationBar.isHidden == false {
self.navigationController?.navigationBar.isHidden = true
} else {
self.navigationController?.navigationBar.isHidden = false
}
}
}
So basically add a UITapGestureRecognizer to your imageView and in the imageTapped function you check if the navigationBar is not hidden then you want to show the image and hide the navigationBar and if you click on the imageView again you want to show the navigationBar again.
So simply add the logic in imageTapped to your dismissFullscreen function.
Related
I created a view controller for a UIBarButtonItem, a toggle on the main controller. but the UIAction button is inactive
import UIKit
class SideMenuContentViewController: UIViewController {
//Manipulation for Dashboard Side Menu Hamburger
let profileButton = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
profileButton.setImage(UIImage(named: "hamburger") , for: .normal)
profileButton.contentMode = .scaleAspectFit
profileButton.translatesAutoresizingMaskIntoConstraints = false
// function performed when the button is tapped
profileButton.addTarget(self, action: #selector(profileButtonTapped(_:)), for: .touchUpInside)
// Add the profile button as the left bar button of the navigation bar
let barbutton = UIBarButtonItem(customView: profileButton)
self.navigationItem.leftBarButtonItem = barbutton
// Set the width and height for the profile button
NSLayoutConstraint.activate([
profileButton.widthAnchor.constraint(equalToConstant: 35.0),
profileButton.heightAnchor.constraint(equalToConstant: 35.0)
])
// Make the profile button become circular
profileButton.layer.cornerRadius = 35.0 / 2
profileButton.clipsToBounds = true
}
#IBAction func profileButtonTapped(_ sender: Any){
}
}
import UIKit
class MainSideViewController: SideMenuContentViewController {
#IBOutlet weak var sideMenuContainer: UIView!
#IBOutlet weak var sideMenuViewLeadingConstraint: NSLayoutConstraint!
#IBOutlet weak var dashBoardViewLeadingConstraint: NSLayoutConstraint!
var sideMenuVisible = false
override func viewDidLoad() {
super.viewDidLoad()
sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
}
#objc func toggleSideMenu(fromViewController: SideMenuContentViewController) {
if(sideMenuVisible){
UIView.animate(withDuration: 0.5, animations: {
// hide the side menu to the left
self.sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
// move the content view (tab bar controller) to original position
self.dashBoardViewLeadingConstraint.constant = 0
self.view.layoutIfNeeded()
})
} else {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.5, animations: {
// move the side menu to the right to show it
self.sideMenuViewLeadingConstraint.constant = 0
// move the content view (tab bar controller) to the right
self.dashBoardViewLeadingConstraint.constant = self.sideMenuContainer.frame.size.width
self.view.layoutIfNeeded()
})
}
sideMenuVisible = !sideMenuVisible
}
}
// THIS IS THE ACTION NOT WORKING
#IBAction override func profileButtonTapped(_ sender: Any){
if let mainVC = self.navigationController?.tabBarController?.parent as? MainSideViewController {
mainVC.toggleSideMenu(fromViewController: self)
}
}
}
I want the action to google in the side menu.
I have a UIView that is showed every time I press a button in another view
#IBOutlet weak var view1: UIView!
#IBOutlet weak var view2: UIView!
#IBAction func showView(_ sender: Any) {
view2.isHidden = false
}
What I want is to add a tap gesture that allows me to hide view2 every time I tap outside of the view and, since those views are draggable, I want the second view not to be tappable when hidden ( so that if I touch under my view I don't risk to move it.
This is what I tried:
var gesture : UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(closeView), name: NSNotification.Name("CloseView"), object: nil)
gesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.closeView))
}
#objc func closeView() {
if view2.isHidden == false {
view2.isUserInteractionEnabled = true
view2.isHidden = false
self.view.removeGestureRecognizer(gesture!)
} else {
view2.isHidden = true
view2.isUserInteractionEnabled = true
self.view.addGestureRecognizer(gesture!)
}
}
let closeTapGesture = UITapGestureRecognizer(target: view, action: #selector(getter: view2.isHidden)
view.addGestureRecognizer(closeTapGesture)
None of this work, how can I do?
You need to check if you actually tapped outside of view2:
var gesture : UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(closeView), name: NSNotification.Name("CloseView"), object: nil)
let gesture = UITapGestureRecognizer(target: self, action: #selector(closeView(_:)))
view.addGestureRecognizer(gesture)
self.gesture = gesture
}
#objc private func closeView(_ tapGestureRecognizer: UITapGestureRecognizer) {
let location = tapGestureRecognizer.location(in: view2)
guard view2.isHidden == false,
!view2.bounds.contains(location) else { //We need to have tapped outside of view 2
return
}
view2.isHidden = true
}
Your tap gesture should only handle closeView .
#objc func closeView() {
view2.isHidden = true
view2.isUserInteractionEnabled = false
gesture?.isEnabled = false
}
And the the button click to show your view2 should call this.
func showView() {
view2.isHidden = false
view2.isUserInteractionEnabled = true
gesture?.isEnabled = true
}
I'm trying to solve this problem but I don't understand how to do it.
I have a view controller in which I have a view (CanvasView) and three buttons. Each button draw a type of shape. I want the user with a click of a button to add a shape with a tap in a certain point of the CanvasView only when the button is clicked.
Is it possible to allow tapGesture only when the button is clicked?
Here is the code:
#IBOutlet weak var CanvasView: CanvasView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
tapGR.numberOfTapsRequired = 2
CanvasView.isUserInteractionEnabled = true
CanvasView.addGestureRecognizer(tapGR)
}
#IBAction func tap(_ sender: UITapGestureRecognizer) {
let tapPoint = sender.location(in: CanvasView)
let shapeView = ShapeSquare(origin: tapPoint)
CanvasView.addSubview(shapeView)
}
#IBAction func DrawSquare(_ sender: UIButton) {
CanvasView.setNeedsDisplay()
}
Keep the gesture disabled until the button is tapped.
Start by making the gesture a property:
var tapGR: UITapGestureRecognizer!
Then update viewDidLoad:
tapGR = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
tapGR.isEnabled = false
Then in your button handler:
tapGR.isEnabled = true
I have a tap gesture recognizer hooked up to my image view. When the image is tapped it becomes full screen and when tapped again it is dismissed. The user has the ability to pinch to zoom the image, however when the image is held down and moved around by the user it shows the background view. I want to hide the background so the view can not be seen until the image is dismissed. I think the images I have provided will explain better than I can through words.
var newImageView: UIImageView!
#IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
self.navigationController?.setNavigationBarHidden(true, animated: true)
let imageView = sender.view as! UIImageView
let scrollView = UIScrollView(frame: self.view.frame)
newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
scrollView.addGestureRecognizer(tap)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3.0
scrollView.addSubview(newImageView)
self.view.addSubview(scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return newImageView;
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
self.navigationController?.setNavigationBarHidden(false, animated: true)
sender.view?.removeFromSuperview()
}
You can simply hide an element with the backgroundElement.isHidden = true property. You can reset the background using backgroundElement.isHidden = false once the view has been dismissed.
Use following method of UIScrollView to check at what Scale it is zoomed.
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print(scale)
if scale == 1.0 {
self.lblDesc.isHidden = false
self.btnDelete.isHidden = false
} else {
}
}
When scale is 1.0 it is normal minimumZoomScale.
Following is method called when you zoom. Use it to hide your elements.
func scrollViewDidZoom(_ scrollView: UIScrollView) {
self.lblDesc.isHidden = true
self.btnDelete.isHidden = true
}
Hope it helps you.
I tried calling tabBarController!.tabBar.hidden = true in viewDidLoad() and it hides the TabBar. However, I tried to set tap gesture and hide the bar on Tap. The parent viewController that has ScrollView inside it with subview (that is connected with IBOutlet as myView)
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
myView.addGestureRecognizer(tap)
}
func handleTap(sender: UITapGestureRecognizer? = nil) {
print("A") // logs successfully
if TabBarHidden == false {
print("B") // logs successfully
//I tried:
tabBarController?.tabBar.hidden = true
// I also tried
tabBarController?.tabBar.alpha = 0
tabBarController?.tabBar.frame.origin.x += 50
hidesBottomBarWhenPushed = true
} else {
...
TabBarHidden = false
}
}
hidden does work when I call it in viewDidLoad as I said, but not if I call in tap gesture function. What may be the problem? What am I missing?
this code totally works for me:
class ViewController: UIViewController {
var tabBarHidden: Bool = false {
didSet {
tabBarController?.tabBar.hidden = tabBarHidden
}
}
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapGestureRecognized(_:)))
view.addGestureRecognizer(tapGestureRecognizer)
}
func tapGestureRecognized(sender: UITapGestureRecognizer) {
tabBarHidden = !tabBarHidden
}
}