Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:36:53+00:00 2026-05-23T16:36:53+00:00

I have a button that’s created via a NIB file. I’ve derived a class

  • 0

I have a button that’s created via a NIB file. I’ve derived a class from UIButton, replaced the class name in the NIB file.

Now my button displays with no background. The text is there, the text font and color are right, and it reacts to taps as expected, but it’s as if the background is transparent. In the NIB, it’s not transparent – I did not change any of the properties other than the class name.

The subclass is trivial – it overrides nothing (for now). Please, what am I doing wrong?

The reason I need a subclass of UIButton is because I want to be able, under certain circumstances, to drag text from the button to elsewhere. If there’s an alternative way to handle drag and drop in a UIKit provided view, I’m willing to hear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:36:53+00:00Added an answer on May 23, 2026 at 4:36 pm

    Honestly not sure what’s wrong with the subclass, but the ‘Net (including SO) is full of cautionary tales about subclassing UIButton, and how you shouldn’t.

    So I’ll go with method swizzling on the four touch processing methods. The following function replaces the provided method of a button with my implementation (taken from the MyButton class), while saving the old one in the system button class under a different selector:

    //Does NOT look at superclasses
    static bool MethodInClass(Class c, SEL sel)
    {
        unsigned n,i ;
        Method *m = class_copyMethodList(c, &n);
        for(i=0;i<n;i++)
        {
            if(sel_isEqual(method_getName(m[i]), sel))
               return true;
        }
        return false;
    }
    
    static void MountMethod(Class &buc, SEL SrcSel, SEL SaveSlotSel)
    {
        IMP OldImp = [buc instanceMethodForSelector:SrcSel];
        IMP NewImp = [[MyButton class] instanceMethodForSelector:SrcSel];
        if(OldImp && NewImp)
        {
            //Save the old implementation. Might conflict if the technique is used
            //independently on many classes in the same hierarchy
            Method SaveMe = class_getInstanceMethod(buc, SaveSlotSel);
            if(SaveMe == NULL)
                class_addMethod(buc, SaveSlotSel, OldImp, "v@:@@");
            else
                method_setImplementation(SaveMe, OldImp);
    
            //Note: the method's original implemenation might've been in the base class
            if(MethodInClass(buc, SrcSel))
            {
                Method SrcMe = class_getInstanceMethod(buc, SrcSel);
                if(SrcMe)
                    method_setImplementation(SrcMe, NewImp);
            }
            else //Add an override in the current class
                class_addMethod(buc, SrcSel, NewImp, "v@:@@");
        }
    }
    

    And call it so:

    Class buc = [bu class];
    MountMethod(buc, @selector(touchesBegan:withEvent:), @selector(MyButton_SavedTouchesBegan:withEvent:));
        MountMethod(buc, @selector(touchesCancelled:withEvent:), @selector(MyButton_SavedTouchesCancelled:withEvent:));
        MountMethod(buc, @selector(touchesEnded:withEvent:), @selector(MyButton_SavedTouchesEnded:withEvent:));
        MountMethod(buc, @selector(touchesMoved:withEvent:), @selector(MyButton_SavedTouchesMoved:withEvent:));
    

    This has the disadvantage of mounting the said methods for all buttons, not just for the ones desired. In the MyButton’s implementation, there’s an additional check if the drag-drop functionality is to be enabled for this particular button. For that I’ve used associated objects.

    One fine point is that touchesXXX methods are implemented in the UIControl class, not in the UIButton. So my first, naive implementation of swizzling would replace the method in UIControl instead of the button class. The current implementation does not assume either way. Also, it makes no assumptions about the run-time class of buttons. Could be UIButton, could be anything (and in real iOS, it’s UIRoundedRectButton).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that is programatically created, it's content is a stack panel
I have a button that plays an audio file on its click listener. If
I have a button that launches the google maps app on my device via
I have a Button that in SSJS send and Email... Now I would if
I have a button that grabs some data from the server using ajax, and
I have a button that writes output to a file. And checks if the
I have a button that when clicked, saves a canvas to an image file
I have a Button that needs to be slide in from the bottom automatically
I have a button that is generating a PDF file on the server and
I have a button that depending on a class value should send different values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.