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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:17:11+00:00 2026-06-11T11:17:11+00:00

I would like to recreate this button using code so it’s a reusable object,

  • 0

Button example

I would like to recreate this button using code so it’s a reusable object, and you can set the minimum width, height, or it stretches to fit the icon and label. Throughout the app, we’ll reuse the button in several areas and it will include a thin rounded rect stroke, a background color, an icon (trans PNG), and a label. We want to make the background color, and stroke color configurable so we can toggle the button on/off.

Example layout over a view area with background pattern


EDIT: Almost working code but the text label block is white and need to resize image to fit in frame and both to be centered.

Custom button code:

#import "CustomButton.h"

@implementation CustomButton

- (id)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title background:(UIColor *)background border:(UIColor *)border 
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self = [UIButton buttonWithType:UIButtonTypeCustom];
        CALayer *layer = [self layer];

        self.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        // background
        if (background) {
            layer.backgroundColor = (__bridge CGColorRef)(background);
        } else {
            layer.backgroundColor = [[UIColor darkGrayColor] CGColor];
        }

        // border
        if (border) {
            layer.borderColor = (__bridge CGColorRef) (border);
        } else {
            layer.borderColor = [[UIColor lightGrayColor] CGColor];
        }
        layer.cornerRadius = 2.0f;
        layer.borderWidth = 0.5f;

        // icon
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
                                  [self addSubview:imageView];

        // text label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 55, 15)];
        titleLabel.font = [[UIFont alloc] fontWithSize:7.00];
                                titleLabel.text = title;
                                [self addSubview:titleLabel];

        [self setFrame:frame];
    }
    return self;
}

@end

EDIT: Updated code block above and got button to appear using the following code in the respective view in viewController:

CGRect buttonFrame = CGRectMake(125, 3, 52, 37);
CustomButton *btnNearby = [[CustomButton alloc] initWithFrame:buttonFrame image:@"map.png" title:@"NEARBY" background:nil border:nil];
[myCustomView addSubview:btnNearby];

The custom button appears but still not formatted properly.

enter image description here


Here is an example icon (white PNG w/ trans) that should appear in center of button.

Example map pin icon to appear in button


Summary of desired functionality:

1) reusable button
2) can have min width/height or override to match width of label and height of image + label
3) has configurable stroke color
4) matches button icon above with stroke + icon + label + background color
5) can change the border color to toggle on/off

  • 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-11T11:17:13+00:00Added an answer on June 11, 2026 at 11:17 am

    I was able to solve this problem and I’m sure it can be refined further but the button now appears as desired in question. See snap of end result, and working code below hopefully to help others.

    Working screenshot:
    enter image description here

    Working code:

    CustomButton.h

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    
    @interface CustomButton : UIButton
    - (id)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title background:(UIColor *)background border:(UIColor *)border;
    @end
    

    CustomButton.m

    #import "CustomButton.h"
    
    @implementation CustomButton
    
    - (id)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title background:(UIColor *)background border:(UIColor *)border 
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            self = [UIButton buttonWithType:UIButtonTypeCustom];
            CALayer *layer = [self layer];
    
            // background
            if (background) {
                layer.backgroundColor = (__bridge CGColorRef)(background);
            } else {
                layer.backgroundColor = [[UIColor darkGrayColor] CGColor];
            }
    
            // border
            if (border) {
                layer.borderColor = (__bridge CGColorRef)(border);
            } else {
                layer.borderColor = [[UIColor lightGrayColor] CGColor];
            }
            layer.cornerRadius = 2.0f;
            layer.borderWidth = 0.5f;
    
            // icon
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,3,20,19)];
            imageView.image = [UIImage imageNamed:image];
            imageView.contentMode  = UIViewContentModeScaleAspectFit;
            [self addSubview:imageView];
    
            // text label
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 14)];
            titleLabel.opaque = NO;
            titleLabel.numberOfLines = 1;
            titleLabel.textAlignment = UITextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:7.00];
            titleLabel.textColor = [UIColor whiteColor];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.text = title;
            [self addSubview:titleLabel];
    
            [self setFrame:frame];
        }
        return self;
    }
    
    @end
    

    Instantiated the button on a UIImage layer that had segue to the View Controller:

    // Add custom button to image view background layer
    CGRect buttonFrame = CGRectMake(125, 3, 50, 35);
    CustomButton *btnNearby = [[CustomButton alloc] initWithFrame:buttonFrame image:@"map.png" title:@"NEARBY" background:nil border:nil];
    [myCustomView addSubview:btnNearby];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to recreate something like this if ( arg1 || arg2 ||
Would like to be able to set colors of headings and such, different font
Would like to know the c# code to actually retrieve the IP type: Static
Would like to know how to hide an div after a set of css3
I would like to recreate the interface below that allows the user to select
I would like to re-create the animation of the delete confirmation button that Apple
I would like my class to be: class NumberedString : public Object { public:
I would like to recreate the swipe functionality of the ios platform. Where the
I am very green at Drupal and I would like to recreate a list
I would like to recreate the look of the following page on my existing

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.