[英]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.我有一个实现“UIView”的 class 和内部
- (void)drawRect:(CGRect)rect
方法我添加了一些自定义按钮。 Inside a UIViewController I declare an instance of this view and i add it to self.view.在 UIViewController 中,我声明了该视图的一个实例并将其添加到 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.如果我在
- (void)viewDidLoad
中执行此操作,则一切正常,但如果我创建一个在按下按钮时调用的方法,并将该视图添加到 self.view,则按钮背景会在几秒钟后显示。 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我需要在 -viewDidLoad 之后将“buttonsView”object 添加到 self.view
drawRect: isn't exactly the place you should be creating subviews (Buttons are subviews) for your view. drawRect:不完全是您应该为您的视图创建子视图(按钮是子视图)的地方。 This should either go into initWithFrame: or awakeFromNib , depending on your view creation method: programmatically or using a nib.
这应该将 go 转换为initWithFrame:或awakeFromNib ,具体取决于您的视图创建方法:以编程方式或使用 nib。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.