Mike Strong
Mike Strong

Reputation: 950

How do I hide UI elements on a tap of the View, using swift?

Just as navigationController?.hidesBarsOnTap = true hides the navigation bar, how do I hide regular UI elements like buttons, labels etc. with a tap of the view? I made a PDF viewer from scratch and now I need a method that will hide the UI elements on the view with a tap, just like the Photos app does on an iPhone.

Here is the code I attempted to use, but no success:

override func viewDidAppear(animated: Bool) {
        var dismissTap = UITapGestureRecognizer(target: self, action: Selector("dismissViewElements:"))

        self.view.addGestureRecognizer(dismissTap)

    }

    func dismissViewElements(gesture: UITapGestureRecognizer){
        if gesture.state == UIGestureRecognizerState.Began {
            self.backButton.alpha = 0
        }
    }

Any suggestions would be very helpful.

Upvotes: 0

Views: 4141

Answers (3)

Abdul Ahmad
Abdul Ahmad

Reputation: 10021

I think I understand what you want. On tap of the view, you want to hide a bunch of "tools" (edit buttons, etc...). The app that comes to mind is the photo viewer app that comes with iPhones, when you tap once, the edit/crop/back buttons etc... all hide.

The easiest way to do this (in my opinion) is to add all these ui controls to a parent view, on a separate "layer". and have that layer be the top layer. When a user taps this view, (as long as its not a button or something inside the parent view) the view disappears (along with all its children buttons/controls/etc.)

I would personally use touchesEnded because its really easy to implement. heres what the touchesEnded method should look like (more or less)

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.hidden = true
        //where parentView is the parent view, whatever you named it
    }

and you should be all set I think

also, I forgot to mention, you should subclass a UIView and have the touchesEnded inside this particular implementation of the UIView and in the storyboard, you can click the UIView that you're using as a parent, and give it a class type of whatever you named the subclass

I pushed up a quick project for you on github to illustrate the idea


Update - make elements appear again

you can do something very similar. Have a parent UIVIew, with a child UIVIew inside of it, and all the buttons/tools inside of the child UIView. This way, on touchesEnded of the parent UIView you can hide the child UIView and on touchesEnded again make the child UIView reappear. all you have to do inside touchesEnded is the following:

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
            childUiView.hidden = !childUiView.hidden
        }

this way, every time you touch it, it will assign hidden to the opposite of what it was.

Upvotes: 2

Duncan C
Duncan C

Reputation: 131481

Your question is awfully vague/broad. What part of this do you need help with? What have you tried?

In very general terms:

Create a tap gesture recognizer. Attach it to the view that you want to tap. Have it call a method in your view controller. Let's call that method toggleControls:

The method needs to take the gesture recognizer as a parameter.

Add a Bool property controlsAreHidden to your VC.

in the toggleControls method, switch the state of controlsAreHidden.

Write code that shows the controls when controlsAreHidden == true, and hides them when controlsAreHidden == false.

Upvotes: 0

facumedica
facumedica

Reputation: 658

You must add a Tap gesture recognizer to your item and then make an outlet/action. In this action you set the alpha or the hidden property.

Upvotes: 0

Related Questions