UITapGestureRecognizer not working on UILabel but working on parent view











up vote
1
down vote

favorite












View hierarchy is like this,



- control (UIView)
- container (UIView)
- icon (UILabel)
- label(UILabel)


If I try to add Tap gesture on control, it works perfectly. If I try to addTap gesture on any of the other items, it doesn't work.



isUserInteractionEnabled is enabled for every element. I also called BringToFront for each element.



I also tried to set UIGestureRecognizerDelegate and detect Touch event, it isn't detecting as well.



Any idea?



Here is the code,



func setGesture() {
// self.isUserInteractionEnabled = true
// containerView.isUserInteractionEnabled = true
label.isUserInteractionEnabled = true
// exclamationMark.isUserInteractionEnabled = true

self.bringSubview(toFront: containerView)
containerView.bringSubview(toFront: exclamationMark)

label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapSavingLabel)))
//exclamationMark.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapExclamationMarkButton)))
//self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selfTapped)))
}

@objc func didTapExclamationMarkButton() {
delegate?.didTapExclamationMarkButton()
}

@objc func didTapSavingLabel() {
delegate?.didTapSavingLabel()
}

@objc func selfTapped() {
print("Self Tapped")
}


If i uncomment the Tap gesture on Self, it works well.










share|improve this question
























  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 9 at 8:30










  • @SamuelLiew - I realize that, and I had created a chat room, however... when I went to invite the OP, I see he has a rep of only 6, which is not high enough to talk in chat.
    – DonMag
    Nov 9 at 13:48

















up vote
1
down vote

favorite












View hierarchy is like this,



- control (UIView)
- container (UIView)
- icon (UILabel)
- label(UILabel)


If I try to add Tap gesture on control, it works perfectly. If I try to addTap gesture on any of the other items, it doesn't work.



isUserInteractionEnabled is enabled for every element. I also called BringToFront for each element.



I also tried to set UIGestureRecognizerDelegate and detect Touch event, it isn't detecting as well.



Any idea?



Here is the code,



func setGesture() {
// self.isUserInteractionEnabled = true
// containerView.isUserInteractionEnabled = true
label.isUserInteractionEnabled = true
// exclamationMark.isUserInteractionEnabled = true

self.bringSubview(toFront: containerView)
containerView.bringSubview(toFront: exclamationMark)

label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapSavingLabel)))
//exclamationMark.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapExclamationMarkButton)))
//self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selfTapped)))
}

@objc func didTapExclamationMarkButton() {
delegate?.didTapExclamationMarkButton()
}

@objc func didTapSavingLabel() {
delegate?.didTapSavingLabel()
}

@objc func selfTapped() {
print("Self Tapped")
}


If i uncomment the Tap gesture on Self, it works well.










share|improve this question
























  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 9 at 8:30










  • @SamuelLiew - I realize that, and I had created a chat room, however... when I went to invite the OP, I see he has a rep of only 6, which is not high enough to talk in chat.
    – DonMag
    Nov 9 at 13:48















up vote
1
down vote

favorite









up vote
1
down vote

favorite











View hierarchy is like this,



- control (UIView)
- container (UIView)
- icon (UILabel)
- label(UILabel)


If I try to add Tap gesture on control, it works perfectly. If I try to addTap gesture on any of the other items, it doesn't work.



isUserInteractionEnabled is enabled for every element. I also called BringToFront for each element.



I also tried to set UIGestureRecognizerDelegate and detect Touch event, it isn't detecting as well.



Any idea?



Here is the code,



func setGesture() {
// self.isUserInteractionEnabled = true
// containerView.isUserInteractionEnabled = true
label.isUserInteractionEnabled = true
// exclamationMark.isUserInteractionEnabled = true

self.bringSubview(toFront: containerView)
containerView.bringSubview(toFront: exclamationMark)

label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapSavingLabel)))
//exclamationMark.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapExclamationMarkButton)))
//self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selfTapped)))
}

@objc func didTapExclamationMarkButton() {
delegate?.didTapExclamationMarkButton()
}

@objc func didTapSavingLabel() {
delegate?.didTapSavingLabel()
}

@objc func selfTapped() {
print("Self Tapped")
}


If i uncomment the Tap gesture on Self, it works well.










share|improve this question















View hierarchy is like this,



- control (UIView)
- container (UIView)
- icon (UILabel)
- label(UILabel)


If I try to add Tap gesture on control, it works perfectly. If I try to addTap gesture on any of the other items, it doesn't work.



isUserInteractionEnabled is enabled for every element. I also called BringToFront for each element.



I also tried to set UIGestureRecognizerDelegate and detect Touch event, it isn't detecting as well.



Any idea?



Here is the code,



func setGesture() {
// self.isUserInteractionEnabled = true
// containerView.isUserInteractionEnabled = true
label.isUserInteractionEnabled = true
// exclamationMark.isUserInteractionEnabled = true

self.bringSubview(toFront: containerView)
containerView.bringSubview(toFront: exclamationMark)

label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapSavingLabel)))
//exclamationMark.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapExclamationMarkButton)))
//self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selfTapped)))
}

@objc func didTapExclamationMarkButton() {
delegate?.didTapExclamationMarkButton()
}

@objc func didTapSavingLabel() {
delegate?.didTapSavingLabel()
}

@objc func selfTapped() {
print("Self Tapped")
}


If i uncomment the Tap gesture on Self, it works well.







ios swift xcode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:16









halfer

14.2k757106




14.2k757106










asked Nov 7 at 13:51









Haseeb Iqbal

66




66












  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 9 at 8:30










  • @SamuelLiew - I realize that, and I had created a chat room, however... when I went to invite the OP, I see he has a rep of only 6, which is not high enough to talk in chat.
    – DonMag
    Nov 9 at 13:48




















  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Nov 9 at 8:30










  • @SamuelLiew - I realize that, and I had created a chat room, however... when I went to invite the OP, I see he has a rep of only 6, which is not high enough to talk in chat.
    – DonMag
    Nov 9 at 13:48


















Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Nov 9 at 8:30




Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Nov 9 at 8:30












@SamuelLiew - I realize that, and I had created a chat room, however... when I went to invite the OP, I see he has a rep of only 6, which is not high enough to talk in chat.
– DonMag
Nov 9 at 13:48






@SamuelLiew - I realize that, and I had created a chat room, however... when I went to invite the OP, I see he has a rep of only 6, which is not high enough to talk in chat.
– DonMag
Nov 9 at 13:48














2 Answers
2






active

oldest

votes

















up vote
0
down vote













I created the outlet in Interface Builder and connect to label var.



Updated to new answer.
I think you have "exclamation" view over the label.



The rest is the next code.



- controller
-- container
--- label


class ViewController: UIViewController {

@IBOutlet weak var container: UIView!
@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

self.label.isUserInteractionEnabled = true
self.view.bringSubviewToFront(self.container)


let tap = UITapGestureRecognizer(target: self, action: #selector(tapPerformed(_:)))
self.label.addGestureRecognizer(tap)

}

@objc func tapPerformed(_ tap: UITapGestureRecognizer) {
debugPrint(#function)
}
}





share|improve this answer























  • Please check the code in Edited question.
    – Haseeb Iqbal
    Nov 7 at 14:48


















up vote
0
down vote













The problem is that one or more of the label's superview has a frame size of 0,0. When that view's .clipsToBounds property is false, it allows its subviews (the label, in this case) to be seen on-screen, but it extends outside the bounds of its superview.



Any view in that condition will not respond to touches / gestures.



Once you solve your layout issues and have the frames properly set up, the Tap Gesture added to the label will work as intended.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190817%2fuitapgesturerecognizer-not-working-on-uilabel-but-working-on-parent-view%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I created the outlet in Interface Builder and connect to label var.



    Updated to new answer.
    I think you have "exclamation" view over the label.



    The rest is the next code.



    - controller
    -- container
    --- label


    class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

    self.label.isUserInteractionEnabled = true
    self.view.bringSubviewToFront(self.container)


    let tap = UITapGestureRecognizer(target: self, action: #selector(tapPerformed(_:)))
    self.label.addGestureRecognizer(tap)

    }

    @objc func tapPerformed(_ tap: UITapGestureRecognizer) {
    debugPrint(#function)
    }
    }





    share|improve this answer























    • Please check the code in Edited question.
      – Haseeb Iqbal
      Nov 7 at 14:48















    up vote
    0
    down vote













    I created the outlet in Interface Builder and connect to label var.



    Updated to new answer.
    I think you have "exclamation" view over the label.



    The rest is the next code.



    - controller
    -- container
    --- label


    class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

    self.label.isUserInteractionEnabled = true
    self.view.bringSubviewToFront(self.container)


    let tap = UITapGestureRecognizer(target: self, action: #selector(tapPerformed(_:)))
    self.label.addGestureRecognizer(tap)

    }

    @objc func tapPerformed(_ tap: UITapGestureRecognizer) {
    debugPrint(#function)
    }
    }





    share|improve this answer























    • Please check the code in Edited question.
      – Haseeb Iqbal
      Nov 7 at 14:48













    up vote
    0
    down vote










    up vote
    0
    down vote









    I created the outlet in Interface Builder and connect to label var.



    Updated to new answer.
    I think you have "exclamation" view over the label.



    The rest is the next code.



    - controller
    -- container
    --- label


    class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

    self.label.isUserInteractionEnabled = true
    self.view.bringSubviewToFront(self.container)


    let tap = UITapGestureRecognizer(target: self, action: #selector(tapPerformed(_:)))
    self.label.addGestureRecognizer(tap)

    }

    @objc func tapPerformed(_ tap: UITapGestureRecognizer) {
    debugPrint(#function)
    }
    }





    share|improve this answer














    I created the outlet in Interface Builder and connect to label var.



    Updated to new answer.
    I think you have "exclamation" view over the label.



    The rest is the next code.



    - controller
    -- container
    --- label


    class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

    self.label.isUserInteractionEnabled = true
    self.view.bringSubviewToFront(self.container)


    let tap = UITapGestureRecognizer(target: self, action: #selector(tapPerformed(_:)))
    self.label.addGestureRecognizer(tap)

    }

    @objc func tapPerformed(_ tap: UITapGestureRecognizer) {
    debugPrint(#function)
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 15:49

























    answered Nov 7 at 14:41









    sergiog90

    9415




    9415












    • Please check the code in Edited question.
      – Haseeb Iqbal
      Nov 7 at 14:48


















    • Please check the code in Edited question.
      – Haseeb Iqbal
      Nov 7 at 14:48
















    Please check the code in Edited question.
    – Haseeb Iqbal
    Nov 7 at 14:48




    Please check the code in Edited question.
    – Haseeb Iqbal
    Nov 7 at 14:48












    up vote
    0
    down vote













    The problem is that one or more of the label's superview has a frame size of 0,0. When that view's .clipsToBounds property is false, it allows its subviews (the label, in this case) to be seen on-screen, but it extends outside the bounds of its superview.



    Any view in that condition will not respond to touches / gestures.



    Once you solve your layout issues and have the frames properly set up, the Tap Gesture added to the label will work as intended.






    share|improve this answer

























      up vote
      0
      down vote













      The problem is that one or more of the label's superview has a frame size of 0,0. When that view's .clipsToBounds property is false, it allows its subviews (the label, in this case) to be seen on-screen, but it extends outside the bounds of its superview.



      Any view in that condition will not respond to touches / gestures.



      Once you solve your layout issues and have the frames properly set up, the Tap Gesture added to the label will work as intended.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The problem is that one or more of the label's superview has a frame size of 0,0. When that view's .clipsToBounds property is false, it allows its subviews (the label, in this case) to be seen on-screen, but it extends outside the bounds of its superview.



        Any view in that condition will not respond to touches / gestures.



        Once you solve your layout issues and have the frames properly set up, the Tap Gesture added to the label will work as intended.






        share|improve this answer












        The problem is that one or more of the label's superview has a frame size of 0,0. When that view's .clipsToBounds property is false, it allows its subviews (the label, in this case) to be seen on-screen, but it extends outside the bounds of its superview.



        Any view in that condition will not respond to touches / gestures.



        Once you solve your layout issues and have the frames properly set up, the Tap Gesture added to the label will work as intended.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 16:00









        DonMag

        15.2k2825




        15.2k2825






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190817%2fuitapgesturerecognizer-not-working-on-uilabel-but-working-on-parent-view%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma