UIView子视图没有调整大小

时间:2012-09-03 12:53:24

标签: ios uiview uiviewcontroller autoresize

我正在开发通用应用程序。我有一个基于视图的应用程序,它有一个视图控制器。我正在添加UIImageView作为子视图。

我的问题是,如果我将ImageView放在viewDidLoad中,它会在iPad中调整大小。

但是如果我在按钮点击事件中动态地添加ImageView来查看控制器,则UIImageView不会按照iPad进行调整大小。相反,它显示的是尺寸为320 x 480的ImageView。

注意:我的视图控制器有setAutoresizesSubviewsYES

我的代码如下:

-(void)buttonTap
{    
    UIImage *img = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"main-bg" ofType:@"png"]];
    UIImageView *imgViewMainBG = [[UIImageView alloc]initWithImage:img];
    imgViewMainBG.frame = CGRectMake(0,0,320,480);
    imgViewMainBG.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:imgViewMainBG];
    [imgViewMainBG release];
    [img release];
    img=nil;         
}

2 个答案:

答案 0 :(得分:2)

AutoresizingMask用于根据superView大小的变化改变所有子视图的大小,每当superView的大小发生变化时,它都不会在添加子视图时调整子视图的大小。

但你可以找到self.view的边界或框架,并根据下面的界限设置imageView的frame属性

 [imgViewMainBG setFrame:CGRectMake(0, 0, self.view.frame.size.width,  
                                    self.view.frame.size.height)];

这样imgViewMainBG在ipad中完全可见

答案 1 :(得分:0)

尝试将图片视图的contentMode属性更改为UIViewContentModeScaleToFill

相关问题