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

  • Home
  • SEARCH
  • 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 7603207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:36:24+00:00 2026-05-30T23:36:24+00:00

I am subclassing UIButton. But I need to know the state that the button

  • 0

I am subclassing UIButton.
But I need to know the state that the button is in to draw the color of button for up or down:

- (void)drawRect:(CGRect)rect{
     if (state==UIControlStateNormal) {
         //draw rect RED
     }
     if (state==UIControlEventTouchDown) 
         //draw rect BLUE
     }
}
  • 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-30T23:36:25+00:00Added an answer on May 30, 2026 at 11:36 pm

    Did you even try looking at the docs?

    1. Goto UIButton Reference
    2. Check the properties
    3. Nothing obvious
    4. See if UIButton has a superclass
    5. Goto UIControl Reference – UIButton‘s superclass
    6. Check the properties
    7. Oh look there is a state property

    Update

    The accepted answer is slightly incorrect and could lead to some annoyingly difficult bug to track down.

    The header for UIControl declares state as

    @property(nonatomic,readonly) UIControlState state;                  // could be more than one state (e.g. disabled|selected). synthesized from other flags.
    

    Now looking up to see how UIControlState is defined we see

    enum {
        UIControlStateNormal       = 0,                       
        UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
        UIControlStateDisabled     = 1 << 1,
        UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
        UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
        UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
    };
    typedef NSUInteger UIControlState;
    

    Therefore as you are dealing with a bit mask you should check for the state appropriately e.g.

    if (self.state & UIControlStateNormal) { ... } 
    

    Update 2

    You could do this by drawing into an image and then setting the image as the background e.g.

    - (void)clicked:(UIButton *)button;
    {
        UIGraphicsBeginImageContext(button.frame.size);
    
        // Draw gradient
    
        UIImage *gradient = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am subclassing a UITableViewCell and in the drawRect: method I want to draw
basically I need an image button, specifically a custom object that: 1) calls a
I'm contemplating subclassing UIScrollView (the way UITextView does) to draw a fairly large amount
After a quick subclassing of Windows.Forms.Label and then noticing that the default Label is
I'm subclassing FileHandler to write a handler that produces correct xml files (meaning, not
I tried to subclass UIButton to include an activity indicator, but when i use
in Objective-C if I need to add additional method to UIButton I can use
Suppose I have a button that I am adding to an annotation object in
When subclassing an CALayer and implementing the drawInContext method, I would assume that any
I'm subclassing the CrudRestController to implement an REST interface. It works fine, but the

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.