What's the easiest way to get tap events on a disabled UIButton?

I have UIButton

a form and want to put it in a disabled state when the form is incomplete. However, I still want to know if the user is trying to click the button even in a disabled state, so that the interface can tell the user that some required fields on the form have not yet been filled in (and maybe scroll down to that field and specify it, etc.) etc.).

There seems to be no easy way to do this. I tried just bind UITapGestureRecognizer

to UIButton

, but it doesn't respond when the button is disabled.

I would like to avoid subclassing UIButton

if possible, unless that's the only way.

+3


source to share


3 answers


Create a backup button. Place it behind the main button. Set the background and text color [UIColor clearColor]

to so that it is not displayed. (You can't just set its alpha value to 0, as that makes it ignore strokes.) In the Builder interface, the back button must be above the main button in the subtask list, for example:

buttons in subview list

Give it the same frame as the main button. If you are using autoplay, select both primary and secondary buttons and create constraints so that all four edges are equal.



When the main button is disabled, touches will go to the return button. When the main button is on, it will capture all touches, but the return button will not.

Connect a backup button to the action so you can detect when it knocked.

+9


source


Based on @rob , I will subclass UIButton

and add a transparent button in front of someone addSubview

on that button.

This custom UIButton will save you a lot of time customizing UI components on your storyboard.



    class TTButton : UIButton {

        // MARK: -
        private lazy var fakeButton : UIButton! = self.initFakeButton()
        private func initFakeButton() -> UIButton {
            let btn = UIButton(frame: self.frame)

            btn.backgroundColor = UIColor.clearColor()
            btn.addTarget(self, action: #selector(self.handleDisabledTouchEvent), forControlEvents: UIControlEvents.TouchUpInside)
            return btn
        }

        override func willMoveToSuperview(newSuperview: UIView?) {
            if (newSuperview != nil) {
                newSuperview!.addSubview(self.fakeButton)
            } else {
                self.fakeButton.removeFromSuperview()
            }

            super.willMoveToSuperview(newSuperview)
        }

        @objc private func handleDisabledTouchEvent() {
            //NSLog("handle disabled touch event. Enabled: \(self.enabled)")
            self.sendActionsForControlEvents(.TouchUpInside)
        }

    }

      

0


source


You have a big misunderstanding of user experience.

If the button is disabled, it must be untranslatable. You cannot click on a disabled button, so it is disabled.

If you want to warn users when this button is clicked (for example, the form is not filled out correctly or completely), you need to enable this button. And just alert users when they click on it, instead of going further with the application logic.

Or you can disable this button until the criteria of the form are met, but show what is wrong with the form using a different method, for example, place exclamation marks next to the text boxes, change the colors of the text box to red, or something like that ...

But never try to add gesture recognizers or hidden back buttons to a disabled button.

Check and let me know if you see a disabled button:

twitter.com/signup

facebook.com/r.php

appleid.apple.com/account

accounts.google.com/SignUp

login.yahoo.com/account/create

signup.live.com/signup

All of the navigation buttons on these websites are always on, and you get feedback on what is wrong when you try to continue.

And here's a really good answer: https://ux.stackexchange.com/a/76306

In short: disabled UI elements that should be non-switchable. Trying to make them interactive when they are disabled is the same to enable them first.

So, for your question, this is just a design issue. Just try styling your button instead of turning it off / on.

-4


source







All Articles