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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:29:23+00:00 2026-05-24T00:29:23+00:00

I have a class that implements UIView and inside – (void)drawRect:(CGRect)rect method I add

  • 0

I have a class that implements “UIView” and inside - (void)drawRect:(CGRect)rect method I add some custom buttons to self. Inside a UIViewController I declare an instance of this view and i add it to self.view.

If i do this in - (void)viewDidLoad, everything works perfect, but if I create a method that is called when I push a button, and add that view to self.view, the buttons background is displayed after a few second. Till then only the name of the button appears written in white.

What do i do wrong?
Regards

@implementation CustomButton

@synthesize isPressed, buttonViewNumber;

- (id)initWithFrame:(CGRect)frame 
          title:(NSString *)title 
            tag:(int)tag
{
self = [super initWithFrame:frame];
if (self) {
    [self setTitle:title forState:UIControlStateNormal];
    [self setTag:tag];
    self.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);


    [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self setBackgroundImage:[[UIImage imageNamed:@"btn_white_add.png"]                                                     stretchableImageWithLeftCapWidth:20.0 topCapHeight:0.0] forState:UIControlStateNormal];

    [self addTarget:self action:@selector(didSelect)          forControlEvents:UIControlEventTouchUpInside];
    self.isPressed = NO;

}
return self;
}

@implementation ButtonsView

@synthesize listOfButtons;

- (id)initWithFrame:(CGRect)frame bundle:(NSArray *)data
{
self = [super initWithFrame:frame];
if (self) {
    self.backgroundColor = [UIColor clearColor];
    listOfNames = [[NSArray arrayWithArray:data] retain];
    listOfButtons = [[NSMutableArray alloc] init];
    currentViewNumber = 0;
    viewNumber = 0;
}
return self;
}


- (void)drawRect:(CGRect)rect
{
CGSize maximumLabelSize = CGSizeMake(300,24);
UIFont *buttonFont = [UIFont fontWithName:@"Helvetica" size:14.0];

float lineSize = 0;
float lineNumber = 0;

NSArray *listOfFrameX = [NSArray arrayWithObjects: 
                         [NSString stringWithFormat:@"%d",FIRST_LINE_Y],
                         [NSString stringWithFormat:@"%d",SECOND_LINE_Y],
                         [NSString stringWithFormat:@"%d",THIRD_LINE_Y],
                         nil];

for (int i = 0; i < [listOfNames count]; i++) {
    CGSize expectedLabelSize = [[[listOfNames objectAtIndex:i] objectForKey:@"name"]  sizeWithFont:buttonFont 
                                                                                  constrainedToSize:maximumLabelSize 
                                                                                     lineBreakMode:UILineBreakModeTailTruncation];
    if ((lineSize + expectedLabelSize.width) > 260) {
        lineNumber++;
        lineSize = 0;
    }
    if (lineNumber > 2) {
        viewNumber ++;
        lineSize = 0;
        lineNumber = 0;
    }

    CGRect newFrame = CGRectMake(lineSize, 
                                  [[listOfFrameX objectAtIndex:lineNumber] floatValue], 
                                  expectedLabelSize.width+30, 
                                  24);

    CustomButton *myButton = [[CustomButton alloc] initWithFrame:newFrame 
                                                         title:[[listOfNames  objectAtIndex:i] objectForKey:@"name"] 
                                                           tag:[[[listOfNames objectAtIndex:i] objectForKey:@"id"] intValue]];

    myButton.buttonViewNumber = viewNumber;

    [listOfButtons addObject:myButton];
    if (viewNumber == 0) {
        [self addSubview:myButton];
    }

    lineSize += expectedLabelSize.width + 40;

    [myButton release];
    }
}

this way it works:

- (void)viewDidLoad
{
[super viewDidLoad];

placeholders = [[NSArray arrayWithObjects:
                something,something, something,something,nil] retain];

CGRect frame = CGRectMake(10, 247, 300, 84);
buttonsView = [[ButtonsView alloc] initWithFrame:frame bundle:placeholders];
[self.view addSubview:buttonsView];

and like this is doesnt work:

- (void)getCompanyNames:(id)result
{
NSArray *listOfCompanies = (NSArray *)result;

CGRect frame = CGRectMake(10, 247, 300, 84);
buttonsView = [[ButtonsView alloc] initWithFrame:frame bundle:listOfCompanies];

[self.view addSubview:buttonsView];

}

i need to add the “buttonsView” object to self.view after the -viewDidLoad

  • 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-24T00:29:24+00:00Added an answer on May 24, 2026 at 12:29 am

    drawRect: isn’t exactly the place you should be creating subviews (Buttons are subviews) for your view. This should either go into initWithFrame: or awakeFromNib, depending on your view creation method: programmatically or using a nib.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I have a zooming UIScrollView that contains a UIView which contains some custom UIViews
I have a class that implements multiple interfaces. I would like to register these
Let's say I have a class that implements the IDisposable interface. Something like this:
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have class method that returns a list of employees that I can iterate
I'm concerned that this is impossible, because +setAnimationDelegate: of UIView is a class method.
I have a custom UIView that generates a set of subviews and display them
I have an Animal class that implements IDomainObject . There is another class, Cat

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.