我有一個自定義的UIView。所以myView類擴展UIView。在initWithFrame:(CGRect)框架方法中,我將UIImage的UIImageView設置爲背景圖像。iPhone:更改UIImageView框架
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.bgImage = [[[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
int stretchableCap = 20;
UIImage *bg = [UIImage imageNamed:@"myImage.png"];
UIImage *stretchIcon = [bg
stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
[self.bgImage setImage:stretchIcon];
}
return self;
}
所以當我創建我的視圖時,一切都很好。
MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
但是,如果我想改變我的觀點尺寸更大或更小:
customView.frame = CGRectMake(10, 10, 50, 50)];
然後我和我的背景圖片的問題。因爲它的大小沒有改變。該怎麼辦 ?何改變圖像大小與視圖框架?
代碼示例,請 – Jim 2012-03-09 09:10:45
customView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; – tim 2012-03-09 09:13:22
我可以只把self.autoresizesSubviews = YES;到initWithFrame方法? – Jim 2012-03-09 09:14:41