我想知道为什么需要两次点击按钮来完成这个UIImageView代码?

时间:2012-12-14 20:41:45

标签: xcode cocoa-touch uiimageview

对于参考:

@interface rjhccViewController : UIViewController{

int myInt;

IBOutlet UIImageView *testcontainer;
    IBOutlet UILabel *heightS;
    IBOutlet UILabel *widthS;
}

-(IBAction) img1;
-(IBAction) img2;

@end

-(IBAction)img1{

UIImage *img = [UIImage imageNamed:@"p.jpeg"];
[testcontainer setImage:img];
[testcontainer setFrame:CGRectMake(0,0,50,50)];
int myInt = (int) img.size.height;
NSString *imgheight = [NSString stringWithFormat:@"%i",[[NSNumber numberWithFloat:img.size.height] intValue]];
heightS.text = imgheight;
}

所以我把它附在一个按钮上。当我单击按钮时,图像显示并报告高度,但容器不会改变大小。当我再次单击该按钮时,它会转到正确的CGRectMake ...不确定为什么?

罗布

1 个答案:

答案 0 :(得分:0)

如果在将最后一个设置为UIImageView的image属性后需要将UIImageView的大小设置为与图像相同,请尝试以下代码:

UIImage *img = [UIImage imageNamed:@"p.jpeg"];
[testcontainer setImage:img];
[testcontainer sizeToFit];