【问题标题】:Auto-resize UIView to fit iPhone 5 screen while maintaining proportion自动调整 UIView 大小以适应 iPhone 5 屏幕,同时保持比例
【发布时间】:2013-07-11 10:21:47
【问题描述】:

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

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

【问题讨论】:

  • 使用(颤抖)自动布局?
  • In 也需要在 iOS5 上工作,所以自动布局不是一个选项。
  • 嗯...似乎您需要使用子类和代码。等一下……
  • 正确使用弹簧和支柱,视图将正确调整大小。

标签: ios objective-c iphone-5 autoresize


【解决方案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]
}

【讨论】:

  • 高度和宽度不一定要相同。他们只需要按比例增长。
  • 我的初始状态(200x200)只是一个例子,初始高度和宽度可以是任何东西。
  • 然后你可以在那里添加逻辑,比如宽度总是需要是高度的三分之一,等等。
  • 我接受这个答案。任何其他(更好的)建议仍然欢迎...... :)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2021-10-31
    • 2011-05-08
    • 1970-01-01
    • 2015-08-10
    • 2022-01-03
    • 2013-11-24
    相关资源
    最近更新 更多