UIView 子视图不调整大小 [英] UIView subview not resizing

查看:28
本文介绍了UIView 子视图不调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a Universal Application. I have a View-based application which has one view controller. I am adding a UIImageView as a subview.

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

My problem is that if I put the ImageView in viewDidLoad, it is getting resized in iPad.

但是如果我在按钮点击事件上动态添加 ImageView 到视图控制器,UIImageView 不会像 iPad 那样调整大小.相反,它显示的是 320 x 480 尺寸的 ImageView.

But if I add the ImageView to view controller dynamically on button tap event, the UIImageView is not getting resized as per iPad. Instead, it is showing ImageView with 320 x 480 dimensions.

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

Note: My View Controller has setAutoresizesSubviews to YES.

我的代码如下:

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

推荐答案

AutoresizingMask 用于在 superView 的大小改变时根据 superView 大小的变化来改变所有子视图的大小,它不会在此时调整子视图的大小添加子视图的时间.

AutoresizingMask is for changing the size of all subviews according to the change in size of the superView whenever the superView's size is changed, it won't resize subviews at the time of adding a subView.

但是你可以找到self.view的bounds或者frame,然后根据这个bounds设置imageView的frame属性,如下图

But you can find the bounds or frame of self.view and set the frame property of imageView according to that bounds like below

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

以便 imgViewMainBG 在 ipad 中完全可见

so that imgViewMainBG is fully visible in ipad

这篇关于UIView 子视图不调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆