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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:32:16+00:00 2026-06-11T21:32:16+00:00

I have a UIImage resizing perfectly using resizableImageWithCapInsets to make a badge containing a

  • 0

I have a UIImage resizing perfectly using resizableImageWithCapInsets to make a badge containing a text label. I’m interested to know is there a way to do this in a storyboard, or in a view created with a nib?

There’s no technical reason to require this. Most of the UI is done in the nib and I would like to keep as much there as possible rather than loading the image, making it resizable, finding the text dimensions, resizing the UIImageView and then applying the label in code.

I can’t make this project iOS6-only yet (deployment target ios5+) so using layout constraints isn’t an option.

  • 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-11T21:32:18+00:00Added an answer on June 11, 2026 at 9:32 pm

    Yes, there is.

    The trick is to override -initWithCoder: and the image setters (setImage:forState: and setBackgroundImage:forState:) to create the resizable versions of the same image you set in the xib. Obviously, they still won’t resize properly in xib itself, but it will be resized properly after initialization. Don’t forget to set NAResizableButton as the custom class of UIButton.

    Sure beats doing it programmatically. 🙂

    Interface:

    #import <UIKit/UIKit.h>
    
    @interface NAResizableButton : UIButton
    
    @end
    

    Implementation:

    #import "NAResizableButton.h"
    
    @implementation NAResizableButton
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self)
        {
            [self setImage:[self imageForState:UIControlStateNormal] forState:UIControlStateNormal];
            [self setImage:[self imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
            [self setImage:[self imageForState:UIControlStateSelected] forState:UIControlStateSelected];
            [self setImage:[self imageForState:UIControlStateDisabled] forState:UIControlStateDisabled];
    
            [self setBackgroundImage:[self backgroundImageForState:UIControlStateNormal] forState:UIControlStateNormal];
            [self setBackgroundImage:[self backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
            [self setBackgroundImage:[self backgroundImageForState:UIControlStateSelected] forState:UIControlStateSelected];
            [self setBackgroundImage:[self backgroundImageForState:UIControlStateDisabled] forState:UIControlStateDisabled];
        }
        return self;
    }
    
    - (void)setImage:(UIImage *)inImage forState:(UIControlState)inState
    {
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2), ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2));
        if ([inImage respondsToSelector:@selector(resizableImageWithCapInsets:)])
        {
            // iOS 5
            inImage = [inImage resizableImageWithCapInsets:edgeInsets];
        }
        else
            inImage = [inImage stretchableImageWithLeftCapWidth:edgeInsets.left topCapHeight:edgeInsets.top];
    
        [super setImage:inImage forState:inState];
    }
    
    - (void)setBackgroundImage:(UIImage *)inImage forState:(UIControlState)inState
    {
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2), ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2));
        if ([inImage respondsToSelector:@selector(resizableImageWithCapInsets:)])
        {
            // iOS 5
            inImage = [inImage resizableImageWithCapInsets:edgeInsets];
        }
        else
            inImage = [inImage stretchableImageWithLeftCapWidth:edgeInsets.left topCapHeight:edgeInsets.top];
    
        [super setBackgroundImage:inImage forState:inState];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIImage that is displaying a grayscale button. Is there a way
I have a UIImage containing an image with a whole bunch of smaller pictures
I have an UIImage that I want to edit (say, make every second row
I am developing an iOS app using XCode 4.2 I have a UIImage variable
I have a UIImage containing a pattern in the center, and the rest of
I have searched the UIImage class for it's designated initializer, but there wasn't one.
I have a UIImage that I'm instantiating using imageWithData: (the data is loaded from
I am trying to process image using Core Image. I have created UIImage category
I have an application which has around 800 images. Is there any way to
I have a UIImage. I have an EAGLView in my app, which is a

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.