2012-04-28 60 views
0

我有一個從視圖控制器調用的uiview子類。它顯示了一切,但我無法調整它。當我把一個斷點視圖的initwithframe方法,它顯示了不變,但視圖控制器是絕對改變它......調整UIView不工作

這裏是我的視圖控制器代碼:

- (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); 
    } 

,這裏是init方法並繪製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]; 
} 

回答

1

把一個斷點initWithFrame:的方法並沒有看正確的地方。在創建視圖時,這隻會被調用一次。

稍後,您通過調用tbox.frame = someFrame來抓取視圖並更改其框架屬性,這相當於調用setFrame:

此行

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

應該表明你確實改變寬度。

+0

是的。但是寬度在屏幕上不會改變 – michaela 2012-04-28 16:33:36