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 6584451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:32:19+00:00 2026-05-25T16:32:19+00:00

I am creating a custom UIButton in my program (not in IB) and I

  • 0

I am creating a custom UIButton in my program (not in IB) and I would like it to show different images on different states, so I did :

UIImage *learnImg=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"01" ofType:@"png"]];
self.learn=[[UIButton buttonWithType:UIButtonTypeCustom] retain];
[self.learn setFrame:CGRectMake(148, 108,254,97)]; 
[[self.learn imageView] setContentMode:UIViewContentModeScaleAspectFit];
[self.learn setImage:learnImg forState:UIControlStateNormal];

UIImage *learnSelected=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]   pathForResource:@"02" ofType:@"png"]];
[self.learn setImage:learnSelected forState:UIControlEventTouchDown];

[self.learn addTarget:self action:@selector(loadLearn:) forControlEvents:UIControlEventTouchUpInside];

The problem is, the two images have different sizes so when I click on the button, the position of the button got ‘translated’ (I believe this is due to the ‘setFrame’ behaviour since the xy location should be relative).

I couldn’t think of a way to fix this, all I want is to make sure the button is right at the position during whatever states it’s in. Is there anyway that I can set a complete set of style for each button state (frame, images etc)? Or any other thoughts?

Thank you!

I finally worked this around by re-setting the center of the button through two action events: UIControlEventTouchDown and UIControlEventTouchCancel.
Thanks all for the comments.

  • 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-25T16:32:20+00:00Added an answer on May 25, 2026 at 4:32 pm

    UIControlEventTouchDown is not a state of the button. Its an event registered by the button. The various states for the UIButton are

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating custom window title. I did it with requestWindowFeature and setFeatureInt but not
I am creating custom images that I later convert to an image pyramid for
I need to implement creating custom gesture(like Gesture Builder in Android ) in my
I am creating 30 UIButtons dynamically for my custom calender in iphone application. I
I'm often creating custom assertion methods for my JUnit tests. eg: public void assertArrays(String[]
I'm creating custom forum software for a site I'm building, which includes 2 tables
I am creating custom cell in my iphone aap and I am also adding
Jython is great for creating custom data structures on need basis, but how to
I have mastered creating custom data types and adding fields with CCK. Then I
I am creating custom membership provider for my asp.net application. I have also created

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.