UIButton touch event overlap





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I need to make UIButton pattern like follwing screenshot. My problem is if i do set frame normally and set image to button frame. Touch event get overlapping.



I am using follwing code,



UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageWithName:@"Triangle1.png"]];
button1.frame = CGRectMake(100.0, 100.0, 150.0, 150.0);
[view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button2.frame = CGRectMake(175.0, 100.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
[button3 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button3.frame = CGRectMake(150.0, 260.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button3];


One my Senior is telling me, i need to use CALayer , I have read but honestely i am not able to understand there stuffs.



Any help is very much appreciated.










share|improve this question

























  • Why don't you set image as button title image ???

    – Sandeep Bhandari
    Feb 20 '17 at 6:12











  • see this stackoverflow.com/questions/30315337/…

    – Anbu.Karthik
    Feb 20 '17 at 6:15











  • Sir you mean i have to set titleEdgeInset ? But still touch even overlapping

    – Mukesh
    Feb 20 '17 at 6:16






  • 2





    Welcome to SO! Please, learn how to ask a good question! stackoverflow.com/help/how-to-ask. What is missing is something more specific about your issue besides not understanding what a CALayer is. (If that's what's the real issue.)

    – dfd
    Feb 20 '17 at 6:17











  • thank you anbu.karthik, i will try

    – Mukesh
    Feb 20 '17 at 6:27


















2















I need to make UIButton pattern like follwing screenshot. My problem is if i do set frame normally and set image to button frame. Touch event get overlapping.



I am using follwing code,



UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageWithName:@"Triangle1.png"]];
button1.frame = CGRectMake(100.0, 100.0, 150.0, 150.0);
[view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button2.frame = CGRectMake(175.0, 100.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
[button3 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button3.frame = CGRectMake(150.0, 260.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button3];


One my Senior is telling me, i need to use CALayer , I have read but honestely i am not able to understand there stuffs.



Any help is very much appreciated.










share|improve this question

























  • Why don't you set image as button title image ???

    – Sandeep Bhandari
    Feb 20 '17 at 6:12











  • see this stackoverflow.com/questions/30315337/…

    – Anbu.Karthik
    Feb 20 '17 at 6:15











  • Sir you mean i have to set titleEdgeInset ? But still touch even overlapping

    – Mukesh
    Feb 20 '17 at 6:16






  • 2





    Welcome to SO! Please, learn how to ask a good question! stackoverflow.com/help/how-to-ask. What is missing is something more specific about your issue besides not understanding what a CALayer is. (If that's what's the real issue.)

    – dfd
    Feb 20 '17 at 6:17











  • thank you anbu.karthik, i will try

    – Mukesh
    Feb 20 '17 at 6:27














2












2








2








I need to make UIButton pattern like follwing screenshot. My problem is if i do set frame normally and set image to button frame. Touch event get overlapping.



I am using follwing code,



UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageWithName:@"Triangle1.png"]];
button1.frame = CGRectMake(100.0, 100.0, 150.0, 150.0);
[view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button2.frame = CGRectMake(175.0, 100.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
[button3 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button3.frame = CGRectMake(150.0, 260.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button3];


One my Senior is telling me, i need to use CALayer , I have read but honestely i am not able to understand there stuffs.



Any help is very much appreciated.










share|improve this question
















I need to make UIButton pattern like follwing screenshot. My problem is if i do set frame normally and set image to button frame. Touch event get overlapping.



I am using follwing code,



UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageWithName:@"Triangle1.png"]];
button1.frame = CGRectMake(100.0, 100.0, 150.0, 150.0);
[view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button2.frame = CGRectMake(175.0, 100.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
[button3 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button3.frame = CGRectMake(150.0, 260.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button3];


One my Senior is telling me, i need to use CALayer , I have read but honestely i am not able to understand there stuffs.



Any help is very much appreciated.







ios objective-c uibutton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 13:05







Mukesh

















asked Feb 20 '17 at 6:11









MukeshMukesh

2,0311328




2,0311328













  • Why don't you set image as button title image ???

    – Sandeep Bhandari
    Feb 20 '17 at 6:12











  • see this stackoverflow.com/questions/30315337/…

    – Anbu.Karthik
    Feb 20 '17 at 6:15











  • Sir you mean i have to set titleEdgeInset ? But still touch even overlapping

    – Mukesh
    Feb 20 '17 at 6:16






  • 2





    Welcome to SO! Please, learn how to ask a good question! stackoverflow.com/help/how-to-ask. What is missing is something more specific about your issue besides not understanding what a CALayer is. (If that's what's the real issue.)

    – dfd
    Feb 20 '17 at 6:17











  • thank you anbu.karthik, i will try

    – Mukesh
    Feb 20 '17 at 6:27



















  • Why don't you set image as button title image ???

    – Sandeep Bhandari
    Feb 20 '17 at 6:12











  • see this stackoverflow.com/questions/30315337/…

    – Anbu.Karthik
    Feb 20 '17 at 6:15











  • Sir you mean i have to set titleEdgeInset ? But still touch even overlapping

    – Mukesh
    Feb 20 '17 at 6:16






  • 2





    Welcome to SO! Please, learn how to ask a good question! stackoverflow.com/help/how-to-ask. What is missing is something more specific about your issue besides not understanding what a CALayer is. (If that's what's the real issue.)

    – dfd
    Feb 20 '17 at 6:17











  • thank you anbu.karthik, i will try

    – Mukesh
    Feb 20 '17 at 6:27

















Why don't you set image as button title image ???

– Sandeep Bhandari
Feb 20 '17 at 6:12





Why don't you set image as button title image ???

– Sandeep Bhandari
Feb 20 '17 at 6:12













see this stackoverflow.com/questions/30315337/…

– Anbu.Karthik
Feb 20 '17 at 6:15





see this stackoverflow.com/questions/30315337/…

– Anbu.Karthik
Feb 20 '17 at 6:15













Sir you mean i have to set titleEdgeInset ? But still touch even overlapping

– Mukesh
Feb 20 '17 at 6:16





Sir you mean i have to set titleEdgeInset ? But still touch even overlapping

– Mukesh
Feb 20 '17 at 6:16




2




2





Welcome to SO! Please, learn how to ask a good question! stackoverflow.com/help/how-to-ask. What is missing is something more specific about your issue besides not understanding what a CALayer is. (If that's what's the real issue.)

– dfd
Feb 20 '17 at 6:17





Welcome to SO! Please, learn how to ask a good question! stackoverflow.com/help/how-to-ask. What is missing is something more specific about your issue besides not understanding what a CALayer is. (If that's what's the real issue.)

– dfd
Feb 20 '17 at 6:17













thank you anbu.karthik, i will try

– Mukesh
Feb 20 '17 at 6:27





thank you anbu.karthik, i will try

– Mukesh
Feb 20 '17 at 6:27












2 Answers
2






active

oldest

votes


















1














Use OBShapedButton library its free of cost, Drag OBShapedButton.h & OBShapedButton.m class in your project.& thereafter OBShapedButton.h in your VC.h file where you want use buttons,



#import "OBShapedButton.h"

@property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;


Objective-C



Drag & place UIButton's on storyboard of your matching pattern change each button Custom class UIButton to OBShapedButton in class identifier & create instance in the or drag outlet in the VC.h file.



[yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];


& for swift you can use BridgeHeader






share|improve this answer
























  • Please add the library link. I would like to upvote it.

    – Mukesh
    Nov 22 '18 at 10:11



















4














Karn suggested OBShapedButton library worked for me in Objective C, Touch events works perfectly.



I have Used OBShapedButton library & its free of cost. Brilliant demo by Ole Begemann




Download library add two folders(having OBShapedButton.h & OBShapedButton.m, UIImage+ColorAtPixel.h , UIImage+ColorAtPixel.m) called OBShapedButton & UIImage+ColorAtPixel to your project & Inside your viewcontroller on storyboard give class attribute instead UIButtons as OBShapedButton then drag the outlet to your YourViewController.h.




#import "OBShapedButton.h"

@property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;


then,



[obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];


For swift you can use BridgingHeader



And In bridging header you can import following two classes



#import "OBShapedButton.h"
#import "UIImage+ColorAtPixel.h"


Drag outlet from storyboard to your viewcontroller.swift file



@IBOutlet weak var obsButtonTest: OBShapedButton!


Set image from stroyboard itself or set it programmatically,



obsButtonTest.setImage(.image_name, for: .normal)


Triangular shape button screen






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',
    autoActivateHeartbeat: false,
    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%2f42337589%2fuibutton-touch-event-overlap%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









    1














    Use OBShapedButton library its free of cost, Drag OBShapedButton.h & OBShapedButton.m class in your project.& thereafter OBShapedButton.h in your VC.h file where you want use buttons,



    #import "OBShapedButton.h"

    @property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;


    Objective-C



    Drag & place UIButton's on storyboard of your matching pattern change each button Custom class UIButton to OBShapedButton in class identifier & create instance in the or drag outlet in the VC.h file.



    [yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];


    & for swift you can use BridgeHeader






    share|improve this answer
























    • Please add the library link. I would like to upvote it.

      – Mukesh
      Nov 22 '18 at 10:11
















    1














    Use OBShapedButton library its free of cost, Drag OBShapedButton.h & OBShapedButton.m class in your project.& thereafter OBShapedButton.h in your VC.h file where you want use buttons,



    #import "OBShapedButton.h"

    @property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;


    Objective-C



    Drag & place UIButton's on storyboard of your matching pattern change each button Custom class UIButton to OBShapedButton in class identifier & create instance in the or drag outlet in the VC.h file.



    [yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];


    & for swift you can use BridgeHeader






    share|improve this answer
























    • Please add the library link. I would like to upvote it.

      – Mukesh
      Nov 22 '18 at 10:11














    1












    1








    1







    Use OBShapedButton library its free of cost, Drag OBShapedButton.h & OBShapedButton.m class in your project.& thereafter OBShapedButton.h in your VC.h file where you want use buttons,



    #import "OBShapedButton.h"

    @property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;


    Objective-C



    Drag & place UIButton's on storyboard of your matching pattern change each button Custom class UIButton to OBShapedButton in class identifier & create instance in the or drag outlet in the VC.h file.



    [yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];


    & for swift you can use BridgeHeader






    share|improve this answer













    Use OBShapedButton library its free of cost, Drag OBShapedButton.h & OBShapedButton.m class in your project.& thereafter OBShapedButton.h in your VC.h file where you want use buttons,



    #import "OBShapedButton.h"

    @property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;


    Objective-C



    Drag & place UIButton's on storyboard of your matching pattern change each button Custom class UIButton to OBShapedButton in class identifier & create instance in the or drag outlet in the VC.h file.



    [yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];


    & for swift you can use BridgeHeader







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 21 '17 at 4:43









    KarnKarn

    305




    305













    • Please add the library link. I would like to upvote it.

      – Mukesh
      Nov 22 '18 at 10:11



















    • Please add the library link. I would like to upvote it.

      – Mukesh
      Nov 22 '18 at 10:11

















    Please add the library link. I would like to upvote it.

    – Mukesh
    Nov 22 '18 at 10:11





    Please add the library link. I would like to upvote it.

    – Mukesh
    Nov 22 '18 at 10:11













    4














    Karn suggested OBShapedButton library worked for me in Objective C, Touch events works perfectly.



    I have Used OBShapedButton library & its free of cost. Brilliant demo by Ole Begemann




    Download library add two folders(having OBShapedButton.h & OBShapedButton.m, UIImage+ColorAtPixel.h , UIImage+ColorAtPixel.m) called OBShapedButton & UIImage+ColorAtPixel to your project & Inside your viewcontroller on storyboard give class attribute instead UIButtons as OBShapedButton then drag the outlet to your YourViewController.h.




    #import "OBShapedButton.h"

    @property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;


    then,



    [obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];


    For swift you can use BridgingHeader



    And In bridging header you can import following two classes



    #import "OBShapedButton.h"
    #import "UIImage+ColorAtPixel.h"


    Drag outlet from storyboard to your viewcontroller.swift file



    @IBOutlet weak var obsButtonTest: OBShapedButton!


    Set image from stroyboard itself or set it programmatically,



    obsButtonTest.setImage(.image_name, for: .normal)


    Triangular shape button screen






    share|improve this answer






























      4














      Karn suggested OBShapedButton library worked for me in Objective C, Touch events works perfectly.



      I have Used OBShapedButton library & its free of cost. Brilliant demo by Ole Begemann




      Download library add two folders(having OBShapedButton.h & OBShapedButton.m, UIImage+ColorAtPixel.h , UIImage+ColorAtPixel.m) called OBShapedButton & UIImage+ColorAtPixel to your project & Inside your viewcontroller on storyboard give class attribute instead UIButtons as OBShapedButton then drag the outlet to your YourViewController.h.




      #import "OBShapedButton.h"

      @property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;


      then,



      [obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];


      For swift you can use BridgingHeader



      And In bridging header you can import following two classes



      #import "OBShapedButton.h"
      #import "UIImage+ColorAtPixel.h"


      Drag outlet from storyboard to your viewcontroller.swift file



      @IBOutlet weak var obsButtonTest: OBShapedButton!


      Set image from stroyboard itself or set it programmatically,



      obsButtonTest.setImage(.image_name, for: .normal)


      Triangular shape button screen






      share|improve this answer




























        4












        4








        4







        Karn suggested OBShapedButton library worked for me in Objective C, Touch events works perfectly.



        I have Used OBShapedButton library & its free of cost. Brilliant demo by Ole Begemann




        Download library add two folders(having OBShapedButton.h & OBShapedButton.m, UIImage+ColorAtPixel.h , UIImage+ColorAtPixel.m) called OBShapedButton & UIImage+ColorAtPixel to your project & Inside your viewcontroller on storyboard give class attribute instead UIButtons as OBShapedButton then drag the outlet to your YourViewController.h.




        #import "OBShapedButton.h"

        @property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;


        then,



        [obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];


        For swift you can use BridgingHeader



        And In bridging header you can import following two classes



        #import "OBShapedButton.h"
        #import "UIImage+ColorAtPixel.h"


        Drag outlet from storyboard to your viewcontroller.swift file



        @IBOutlet weak var obsButtonTest: OBShapedButton!


        Set image from stroyboard itself or set it programmatically,



        obsButtonTest.setImage(.image_name, for: .normal)


        Triangular shape button screen






        share|improve this answer















        Karn suggested OBShapedButton library worked for me in Objective C, Touch events works perfectly.



        I have Used OBShapedButton library & its free of cost. Brilliant demo by Ole Begemann




        Download library add two folders(having OBShapedButton.h & OBShapedButton.m, UIImage+ColorAtPixel.h , UIImage+ColorAtPixel.m) called OBShapedButton & UIImage+ColorAtPixel to your project & Inside your viewcontroller on storyboard give class attribute instead UIButtons as OBShapedButton then drag the outlet to your YourViewController.h.




        #import "OBShapedButton.h"

        @property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;


        then,



        [obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];


        For swift you can use BridgingHeader



        And In bridging header you can import following two classes



        #import "OBShapedButton.h"
        #import "UIImage+ColorAtPixel.h"


        Drag outlet from storyboard to your viewcontroller.swift file



        @IBOutlet weak var obsButtonTest: OBShapedButton!


        Set image from stroyboard itself or set it programmatically,



        obsButtonTest.setImage(.image_name, for: .normal)


        Triangular shape button screen







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '18 at 13:48

























        answered Feb 20 '17 at 7:31









        MukeshMukesh

        2,0311328




        2,0311328






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42337589%2fuibutton-touch-event-overlap%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)