Ios 自动调整UIView的大小以适应iPhone 5屏幕,同时保持比例

Ios 自动调整UIView的大小以适应iPhone 5屏幕,同时保持比例,ios,objective-c,iphone-5,autoresize,Ios,Objective C,Iphone 5,Autoresize,我的情节提要配置为iPhone4分辨率,在iPhone5上运行时,我希望UIView变大(高度和宽度) 现在的问题是,视图只会越来越高,而不是越来越宽。要实现这一点,自动调整大小配置应该是什么 您可能需要使用UIView的子类,并覆盖setFrame:方法来捕获帧更改 - (void)setFrame:(CGRect)frame { frame.size.width = frame.size.height; // Make the *width* always equal to the

我的<代码>情节提要配置为iPhone4分辨率,在iPhone5上运行时,我希望UIView变大(高度和宽度)

现在的问题是,视图只会越来越高,而不是越来越宽。要实现这一点,自动调整大小配置应该是什么


您可能需要使用
UIView
的子类,并覆盖
setFrame:
方法来捕获帧更改

- (void)setFrame:(CGRect)frame
{
    frame.size.width = frame.size.height; // Make the *width* always equal to the *height*. Logic could go here, etc.
    [super setFrame:frame]
}

参考屏幕的高度,并使用一些填充值来设置您的图幅。下面是设置名为“yourShapeView”的UIView框架的代码:

// Get the frame of the screen
CGRect screenFrame = [[UIScreen mainScreen] bounds];

// Set padding from the top and bottom of the shape
CGFloat verticalPadding = 40.0f;
CGFloat horizontalPadding = 20.0f;

// Get the height/width values
CGFloat height = screenFrame.size.height - (verticalPadding * 2);
CGFloat width = screenFrame.size.width - (horizontalPadding * 2);

// Set the size of your shape based on the height and padding
yourShapeView.frame = CGRectMake(horizontalPadding, verticalPadding, width, height);

使用(抖动)自动布局?In也需要在iOS5上工作,所以自动布局不是一个选项。嗯。。。看起来您需要使用子类和代码。请稍等……正确使用弹簧和支柱,视图将正确调整大小。高度和宽度不一定需要相同。它们只需要按比例增长。我的初始状态(200x200)只是一个例子,初始高度和宽度可以是任何东西。然后你可以在那里添加逻辑,比如说宽度总是需要高度的三分之一,等等。我接受这个答案。不过,我们仍然欢迎任何其他(更好的)建议:)