【问题标题】:Constraints set on xib file not working for ipad ios在 xib 文件上设置的约束不适用于 ipad ios
【发布时间】:2016-04-18 16:02:49
【问题描述】:

我有一个要在 iphone 和 ipad 上运行的应用程序。我正在使用 xib 文件和自动布局。该布局适用于所有 iphone 模拟器,但不适用于 ipad。我有一个覆盖整个屏幕的 UIImageView,我希望它完全覆盖 iphone 和 ipad 的所有屏幕。所以我设置了以下约束:

其中 new_BG.png 是背景 UIImageView。

但是当我在 ipad 模拟器上运行应用程序时,图像只覆盖了屏幕的一部分。这是一个屏幕截图:

我需要添加哪些约束才能使图像覆盖整个屏幕? xib图片:

还有一件事。我尝试在代码中设置图像视图的框架,使其等于屏幕尺寸的高度和宽度,如下所示:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
new_BG.frame = CGRectMake(0, 0, screenWidth, screenHeight);

但在 ipad 上运行时,它仍然只覆盖屏幕的左上角。然后我将宽度和高度更改为 100,在 ipad 上运行时仍然没有变化。

【问题讨论】:

  • 请出示xib的截图。
  • 好的,添加xib的图片

标签: ios xcode autolayout interface-builder


【解决方案1】:
  1. 您的约束集是多余的 - 您可以将 .centerX 和 .centerY 约束替换为单个关系:new_BG.png.bottom - Superview.Bottom

  2. 如果 UIImageView 的 Mode 属性设置为左上角,它可能会以这种方式显示其 UIImage。

  3. 由于您的 UI 元素也有错误的布局,我认为问题不仅在于 UIImageView(如果您的图像包含自身描绘的 UI 元素)

【讨论】:

  • 好的,我修复了约束。 UIImageview 设置为按比例填充。
【解决方案2】:

不要给出任何中心约束,例如中心 x 或中心 y。只需给leading,trailing,bottom and top。这将覆盖 iphone 和 ipad 上的全屏。

我知道你正在使用 uiview 的自定义导航栏。所以要限制它 - leading,trailing,top and fix height

希望这会奏效。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2021-02-23
    • 2021-01-14
    • 2023-04-04
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2014-12-08
    相关资源
    最近更新 更多