gpt4 book ai didi

iphone - UIView 上不显示背景图像

转载 作者:行者123 更新时间:2023-11-29 05:01:37 26 4
gpt4 key购买 nike

我有一个实现“UIView”的类,并在 - (void)drawRect:(CGRect)rect 方法中向 self 添加一些自定义按钮。在 UIViewController 中,我声明了该 View 的一个实例,并将其添加到 self.view 中。

如果我在 - (void)viewDidLoad 中执行此操作,一切都会完美,但如果我创建一个在按下按钮时调用的方法,并将该 View 添加到 self.view,则几秒钟后显示按钮背景。直到那时,只有按钮的名称以白色显示。

我做错了什么?问候

@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];
}
}

这样就可以了:

- (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];

像这样是行不通的:

- (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];

}

我需要在 -viewDidLoad 之后将“buttonsView”对象添加到 self.view

最佳答案

drawRect:不完全是您应该为 View 创建 subview (按钮是 subview )的地方。这应该进入 initWithFrame:awakeFromNib,具体取决于您的 View 创建方法:以编程方式或使用 Nib 。

关于iphone - UIView 上不显示背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6799598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com