0

我有一个从视图控制器调用的 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子类的init方法和draw方法

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

1 回答 1

1

放置断点initWithFrame:不是寻找的正确位置。创建视图时只会调用一次。

稍后,您将通过调用来获取视图并更改其 frame 属性,tbox.frame = someFrame这等效于调用setFrame:

这条线

NSLog(@"%f", tbox.frame.size.width);

应该表明你确实改变了宽度。

于 2012-04-28T15:54:30.810 回答