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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:23:42+00:00 2026-06-12T18:23:42+00:00

I have a UIButton that has a default image, and another image for highlight/selected

  • 0

I have a UIButton that has a default image, and another image for highlight/selected image. When pressing the button, the button’s image changes to the highlighted, then to the selected if the touchUp was inside. The usual stuff.. All setup within IB.

However, I am trying to set a label over the button, in a very tricky place (not aligned, hard coded).

I tried adding a Label over the button in IB. Problem: I need the label’s text color to change as the button’s control state changes.

So, I created a UIButton subclass, added a UILabel Outlet, and by overriding the following methods:

- (void)touchesBegan/Moved/Cancelled/Ended:...;
- (void)setSelected:...

I was able to achieve what I want… BUT! When I quickly click the button, the change is not reflected. and sometimes it doesn’t work properly… I even used asynchronous calls… No use.

So, I headed to the UIButton‘s titleLabel. I tried to use it with no luck.

So, I tried UIButton setTitle: forState:, no use … Help?


Extra Details:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self.titleLabel setFrame:CGRectMake(0, 0, 100, 100)];
        [self.titleLabel setText:@"THE TITLE LABEL"];
        [self.titleLabel setHidden:NO];

        [self.imageView setAlpha:0.2f];
        NSLog(@"%@", self.subviews);


        [self setTitle:@"DEFAULT!!" forState:UIControlStateNormal];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1];

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1];

}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    [self performSelector:@selector(check) withObject:nil afterDelay:0.1];
}

- (void)check {
    if (self.isSelected || self.state == UIControlStateHighlighted || self.state == UIControlStateSelected) {
        [_label setHighlighted:YES];
    } else {
        [_label setHighlighted:NO];
    }
}

OUTPUT:

(
    "<UIImageView: 0x8b24930; frame = (0 0; 243 39); clipsToBounds = YES; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b248e0>>",
    "<UIButtonLabel: 0x8b247a0; frame = (0 0; 100 100); text = 'THE TITLE LABEL'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8b25000>>"
)
  • 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-06-12T18:23:47+00:00Added an answer on June 12, 2026 at 6:23 pm

    The best answer I found, after all these months, is to override:

    - (CGRect)titleRectForContentRect:(CGRect)contentRect;
    

    To position the label the way I want. This makes me able to make use of:

    [self setTitleColor:[UIColor offWhiteColor] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateSelected];
    [self setTitleColor:[UIColor burntCarbonColor] forState:UIControlStateHighlighted];
    

    For the more curious:

    - (CGRect)titleRectForContentRect:(CGRect)contentRect {
        CGSize margin = CGSizeMake(15.f, 5.f);
        CGRect clay = contentRect;
        clay = CGRectInset(clay, margin.width, 0.f);
        clay.origin.x += margin.width;
        clay.origin.y += margin.height;
    
        return clay;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The UIButton has a normal/default, highlighted, and selected image. I then have a IBAction
I have a UITableViewCell that has 2 UIButton instances. Both buttons have a default
Is it possible to have a custom UIButton that has an image covering only
I have this UIButton, he has an image, on normal iPhone it works fine,
-I have a UIView. -This UIView has a UIButton that when clicked makes a
I have, for example, a UIButton that I placed in IB. This UIButton has
I have a UIView Subclass that has a frame/image with buttons in it. I'm
I have a custom class, and that class has a UIButton instance variable. I
I have an element that needs to behave like UIButton but it has several
I have a custom UIButton that has the label Name. When the user clicks

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.