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

时间:2013-07-12 19:06:54

标签: ios objective-c iphone-5 autoresize

我的StoryBoard配置为iPhone4分辨率,在iPhone 5上运行时,我希望UIView变得更大(高度和宽度)。

现在的问题是视图只会变得更高而不是更宽。为实现这一目标,自动调整大小配置应该是什么?

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

您可能需要使用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]
}

答案 1 :(得分:1)

引用屏幕的高度并使用一些填充值来设置视图框。下面是设置名为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);