Why shouldn't I subclass a UIButton?



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








34















I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.



What are the negatives of subclassing a UIButton? And I know it's vague, but what are other alternatives to subclassing a UIButton?










share|improve this question



















  • 1





    What's your reason to subclass an UIButton? Specify button type custom. What else is needed?

    – ott--
    Nov 2 '12 at 20:07











  • I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing.

    – SirRupertIII
    Nov 2 '12 at 20:12






  • 2





    There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof.

    – Feloneous Cat
    Dec 12 '12 at 19:55











  • Subclassing UIButton is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) UIControl subclass is always better idea.

    – Jakub Truhlář
    Oct 6 '16 at 20:05

















34















I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.



What are the negatives of subclassing a UIButton? And I know it's vague, but what are other alternatives to subclassing a UIButton?










share|improve this question



















  • 1





    What's your reason to subclass an UIButton? Specify button type custom. What else is needed?

    – ott--
    Nov 2 '12 at 20:07











  • I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing.

    – SirRupertIII
    Nov 2 '12 at 20:12






  • 2





    There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof.

    – Feloneous Cat
    Dec 12 '12 at 19:55











  • Subclassing UIButton is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) UIControl subclass is always better idea.

    – Jakub Truhlář
    Oct 6 '16 at 20:05













34












34








34


7






I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.



What are the negatives of subclassing a UIButton? And I know it's vague, but what are other alternatives to subclassing a UIButton?










share|improve this question
















I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.



What are the negatives of subclassing a UIButton? And I know it's vague, but what are other alternatives to subclassing a UIButton?







objective-c ios uibutton uikit subclass






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 2 '12 at 20:21









justin

97.5k12161216




97.5k12161216










asked Nov 2 '12 at 19:55









SirRupertIIISirRupertIII

5,0161659102




5,0161659102







  • 1





    What's your reason to subclass an UIButton? Specify button type custom. What else is needed?

    – ott--
    Nov 2 '12 at 20:07











  • I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing.

    – SirRupertIII
    Nov 2 '12 at 20:12






  • 2





    There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof.

    – Feloneous Cat
    Dec 12 '12 at 19:55











  • Subclassing UIButton is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) UIControl subclass is always better idea.

    – Jakub Truhlář
    Oct 6 '16 at 20:05












  • 1





    What's your reason to subclass an UIButton? Specify button type custom. What else is needed?

    – ott--
    Nov 2 '12 at 20:07











  • I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing.

    – SirRupertIII
    Nov 2 '12 at 20:12






  • 2





    There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof.

    – Feloneous Cat
    Dec 12 '12 at 19:55











  • Subclassing UIButton is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) UIControl subclass is always better idea.

    – Jakub Truhlář
    Oct 6 '16 at 20:05







1




1





What's your reason to subclass an UIButton? Specify button type custom. What else is needed?

– ott--
Nov 2 '12 at 20:07





What's your reason to subclass an UIButton? Specify button type custom. What else is needed?

– ott--
Nov 2 '12 at 20:07













I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing.

– SirRupertIII
Nov 2 '12 at 20:12





I was asking more just if I want to subclass in the future, why I shouldn't do it. But right now I will have about 20 buttons in my app that have the same background, font, padding size around the text, etc. So I wanted to same some time and code by subclassing.

– SirRupertIII
Nov 2 '12 at 20:12




2




2





There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof.

– Feloneous Cat
Dec 12 '12 at 19:55





There is no reason you can't subclass UIButton. In fact, I do it so that I can use IB to define the look of my button. Why? Design flexibility. Many people have subclassed UIButton. I've subclassed UIButton. This belief that it can not is something that gets repeated but lacks proof.

– Feloneous Cat
Dec 12 '12 at 19:55













Subclassing UIButton is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) UIControl subclass is always better idea.

– Jakub Truhlář
Oct 6 '16 at 20:05





Subclassing UIButton is as other components totally fine. But for more complex subclassing (like custom labels, imageViews etc.) UIControl subclass is always better idea.

– Jakub Truhlář
Oct 6 '16 at 20:05












3 Answers
3






active

oldest

votes


















36














The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.



In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.



One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.



edit, your own factory method



I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Category so it's available directly on UIButton.



@interface UIButton ( MyCompanyFactory )
+(UIButton *) buttonWithMyCompanyStyles;
@end
@implementation UIButton
+(UIButton *) buttonWithMyCompanyStyles
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
// [theButton set...
return theButton;

@end





share|improve this answer

























  • although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

    – justin
    Nov 2 '12 at 20:41












  • @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

    – Chris Trahey
    Nov 2 '12 at 21:21






  • 3





    I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

    – justin
    Nov 2 '12 at 21:27











  • How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

    – Jakub Turcovsky
    Aug 21 '14 at 17:51











  • @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

    – Chris Trahey
    Aug 21 '14 at 19:56


















15














It's because UIButton is kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame: (and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustom as your starting point for a UIButton subclass.




On implementing a subclass of UIButton






share|improve this answer




















  • 1





    @Nate, Fixed it.

    – Iulian Onofrei
    Nov 15 '18 at 15:06


















11














If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:



[mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];


UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.






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%2f13202161%2fwhy-shouldnt-i-subclass-a-uibutton%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    36














    The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.



    In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.



    One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.



    edit, your own factory method



    I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Category so it's available directly on UIButton.



    @interface UIButton ( MyCompanyFactory )
    +(UIButton *) buttonWithMyCompanyStyles;
    @end
    @implementation UIButton
    +(UIButton *) buttonWithMyCompanyStyles
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // [theButton set...
    return theButton;

    @end





    share|improve this answer

























    • although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

      – justin
      Nov 2 '12 at 20:41












    • @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

      – Chris Trahey
      Nov 2 '12 at 21:21






    • 3





      I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

      – justin
      Nov 2 '12 at 21:27











    • How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

      – Jakub Turcovsky
      Aug 21 '14 at 17:51











    • @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

      – Chris Trahey
      Aug 21 '14 at 19:56















    36














    The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.



    In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.



    One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.



    edit, your own factory method



    I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Category so it's available directly on UIButton.



    @interface UIButton ( MyCompanyFactory )
    +(UIButton *) buttonWithMyCompanyStyles;
    @end
    @implementation UIButton
    +(UIButton *) buttonWithMyCompanyStyles
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // [theButton set...
    return theButton;

    @end





    share|improve this answer

























    • although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

      – justin
      Nov 2 '12 at 20:41












    • @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

      – Chris Trahey
      Nov 2 '12 at 21:21






    • 3





      I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

      – justin
      Nov 2 '12 at 21:27











    • How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

      – Jakub Turcovsky
      Aug 21 '14 at 17:51











    • @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

      – Chris Trahey
      Aug 21 '14 at 19:56













    36












    36








    36







    The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.



    In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.



    One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.



    edit, your own factory method



    I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Category so it's available directly on UIButton.



    @interface UIButton ( MyCompanyFactory )
    +(UIButton *) buttonWithMyCompanyStyles;
    @end
    @implementation UIButton
    +(UIButton *) buttonWithMyCompanyStyles
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // [theButton set...
    return theButton;

    @end





    share|improve this answer















    The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.



    In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.



    One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.



    edit, your own factory method



    I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Category so it's available directly on UIButton.



    @interface UIButton ( MyCompanyFactory )
    +(UIButton *) buttonWithMyCompanyStyles;
    @end
    @implementation UIButton
    +(UIButton *) buttonWithMyCompanyStyles
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // [theButton set...
    return theButton;

    @end






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 19 '14 at 20:44









    Stan James

    1,5821331




    1,5821331










    answered Nov 2 '12 at 20:01









    Chris TraheyChris Trahey

    17.2k13251




    17.2k13251












    • although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

      – justin
      Nov 2 '12 at 20:41












    • @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

      – Chris Trahey
      Nov 2 '12 at 21:21






    • 3





      I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

      – justin
      Nov 2 '12 at 21:27











    • How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

      – Jakub Turcovsky
      Aug 21 '14 at 17:51











    • @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

      – Chris Trahey
      Aug 21 '14 at 19:56

















    • although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

      – justin
      Nov 2 '12 at 20:41












    • @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

      – Chris Trahey
      Nov 2 '12 at 21:21






    • 3





      I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

      – justin
      Nov 2 '12 at 21:27











    • How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

      – Jakub Turcovsky
      Aug 21 '14 at 17:51











    • @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

      – Chris Trahey
      Aug 21 '14 at 19:56
















    although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

    – justin
    Nov 2 '12 at 20:41






    although important to design, i'm not sure "favor composition" is really a good answer to the question because UIButton is actually idiosyncratic in this regard. i certainly won't downvote because your post does make good points (in fact, i upvoted).

    – justin
    Nov 2 '12 at 20:41














    @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

    – Chris Trahey
    Nov 2 '12 at 21:21





    @justin, I'm always in favor of good conversation! Do you blog anywhere, or have a link to a related discussion somewhere?

    – Chris Trahey
    Nov 2 '12 at 21:21




    3




    3





    I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

    – justin
    Nov 2 '12 at 21:27





    I've added a link from Cocoa-Dev lists in my answer on the subject. If you want to reach me, you're certainly welcome to message me privately or publicly via twitter (handle is in my SO profile). I don't blog - SO is my primary coding water-cooler. I keep a low profile.

    – justin
    Nov 2 '12 at 21:27













    How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

    – Jakub Turcovsky
    Aug 21 '14 at 17:51





    How can i apply this category to a UIButton added to a UIViewController using storyboard? I tried importing the category in the UIViewControllers.m file, but it didn't work. Am I missing anything?

    – Jakub Turcovsky
    Aug 21 '14 at 17:51













    @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

    – Chris Trahey
    Aug 21 '14 at 19:56





    @2rec unfortunately you can't tell a Storyboard to use a custom factory method to create the buttons in it. My recommendation is, if you want a re-usable pre-determined set of button styles in Storyboard-created buttons, just use copy/paste in the storyboards, or create (instead of a factory method) a UI-configuring method as a category on UIButton, and then call that method on your buttons in viewDidLoad or viewDidAppear

    – Chris Trahey
    Aug 21 '14 at 19:56













    15














    It's because UIButton is kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame: (and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustom as your starting point for a UIButton subclass.




    On implementing a subclass of UIButton






    share|improve this answer




















    • 1





      @Nate, Fixed it.

      – Iulian Onofrei
      Nov 15 '18 at 15:06















    15














    It's because UIButton is kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame: (and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustom as your starting point for a UIButton subclass.




    On implementing a subclass of UIButton






    share|improve this answer




















    • 1





      @Nate, Fixed it.

      – Iulian Onofrei
      Nov 15 '18 at 15:06













    15












    15








    15







    It's because UIButton is kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame: (and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustom as your starting point for a UIButton subclass.




    On implementing a subclass of UIButton






    share|improve this answer















    It's because UIButton is kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame: (and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustom as your starting point for a UIButton subclass.




    On implementing a subclass of UIButton







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 15:05









    Iulian Onofrei

    4,16534380




    4,16534380










    answered Nov 2 '12 at 20:20









    justinjustin

    97.5k12161216




    97.5k12161216







    • 1





      @Nate, Fixed it.

      – Iulian Onofrei
      Nov 15 '18 at 15:06












    • 1





      @Nate, Fixed it.

      – Iulian Onofrei
      Nov 15 '18 at 15:06







    1




    1





    @Nate, Fixed it.

    – Iulian Onofrei
    Nov 15 '18 at 15:06





    @Nate, Fixed it.

    – Iulian Onofrei
    Nov 15 '18 at 15:06











    11














    If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:



    [mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];


    UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.






    share|improve this answer



























      11














      If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:



      [mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];


      UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.






      share|improve this answer

























        11












        11








        11







        If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:



        [mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];


        UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.






        share|improve this answer













        If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:



        [mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];


        UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 10 '15 at 19:13









        James Matthew MudgettJames Matthew Mudgett

        16828




        16828



























            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%2f13202161%2fwhy-shouldnt-i-subclass-a-uibutton%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20