开发者

Background image not displayed on UIView

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜