我有一个uiview子类,我从一个视图控制器调用。它显示了一切,但我无法调整大小。当我在视图的initwithframe方法上放置一个断点时,它显示没有变化,但视图控制器肯定会改变它...
这是我的视图控制器代码:
- (void)resizeWithTag:(int)tag wasTouched:(BOOL)touched
{
GameBox *tbox = (GameBox *)[self.view viewWithTag:tag];
CGRect frame = tbox.frame;
frame = CGRectMake(0, 0, 300, 300);
tbox.frame = frame;
[tbox setNeedsDisplay];
NSLog(@"%f", tbox.frame.size.width);
}
这是uiview子类的初始化方法和绘制方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];
name = [[UILabel alloc] init];
self.autoresizesSubviews = YES;
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
int border = 3;
UIImage *image = [UIImage imageNamed:@"Sunflower.jpeg"];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1);
CGContextStrokeRectWithWidth(context, CGRectMake(0, 0, rect.size.width, rect.size.height), border);
[self attachImage:image inRect:rect inBorder:border];
[name setFrame:CGRectMake(0 + border/2, rect.size.height-10-border/2, rect.size.width-(border), 10)];
[name setFont:[UIFont fontWithName:@"Helvetica" size:9.0]];
[name setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.0]];
name.textAlignment = UITextAlignmentCenter;
[self addSubview:name];
}
答案 0 :(得分:1)
在initWithFrame:
中放置一个断点不是正确的地方。创建视图时,只会调用一次。
稍后您通过调用tbox.frame = someFrame
来抓取视图并更改其框架属性,这相当于调用setFrame:
。
这一行
NSLog(@"%f", tbox.frame.size.width);
应该表明你确实改变了宽度。