【问题标题】:Set UIView height depending on screen size根据屏幕大小设置 UIView 高度
【发布时间】:2013-03-01 01:10:04
【问题描述】:

我在桌子下面有一个 UIView。 我必须为 iPhone 4S 或 5 设置 UIView 高度。 我尝试使用

- (void) regolaViewPeriPhone {
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            CGRect frame = CGRectMake(0, 210, 320, 245);
            _pickerContainerView.frame = frame;
        }
        if(result.height == 568)
        {
            CGRect frame = CGRectMake(0, 210, 320, 290);
            _pickerContainerView.frame = frame;
        }
    }
}

但它不起作用,什么是正确的方法?谢谢!

【问题讨论】:

  • regolaViewPeriPhone 被调用了吗?

标签: ios xcode uiview screen screen-size


【解决方案1】:

[ [ UIScreen mainScreen ] applicationFrame ]

【讨论】:

  • 但实际上,您的包含视图控制器应该为您处理这个问题。检查您在 IB 中的自动调整大小标志...
  • 所以有一个封闭的滚动视图?
  • 你确定你的视图是在桌子后面而不是里面?
【解决方案2】:

这是“根据屏幕大小设置 UIView 高度”的方法

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
  //Do u r stuff here for Iphone 5
}
else if(screenBounds.size.height==480)
{
  //Do u r stuff here for Iphone 4s
}

//如果我的答案是正确的,那就投我一票吧……

【讨论】:

  • 没有。不要这样做。切勿在代码中使用硬编码值。如果屏幕尺寸发生变化,您将遇到问题。另外,我认为这不能回答问题。
  • Pandu 的建议有效!您是否建议使用“applicationFrame”而不是“bounds”?
  • 如果您需要应用程序的大小,请使用 applicationFrame。这是正确的框架。它还反映了是否有隐藏状态栏或双高状态栏(在通话期间或使用网络共享时)。您应该尝试找出您的 NIB 中有什么问题导致您的视图无法自动正确调整大小。然后根据视图大小编写布局代码。
  • 如果你打了减号,请提供你自己的正确答案。否则我们知道什么不该做,但我们不知道该做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 2020-11-22
  • 2016-12-11
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多