【问题标题】:UIImageView initWithFrame not correct size when using self.view.frame使用 self.view.frame 时 UIImageView initWithFrame 大小不正确
【发布时间】:2014-05-12 01:20:57
【问题描述】:

我遇到了一个问题,当我以编程方式创建多个 UIImageView 并为它们提供主视图的框架时,它们最终的大小只有一半。我正在模拟器中的 3.5 英寸视网膜屏幕上构建应用程序。我检查了主视图的框架,它(横向)480pts 宽,320pts 高,原点位于 (0,0)。它应该填充与主视图相同的屏幕部分(下图中的蓝色)。我确定我错过了一些超级简单的东西,但我只是没有看到它,并且四处寻找也没有得到答案。

提前感谢您的帮助。

【问题讨论】:

  • 显示你的相关代码,否则没人能帮你找到问题。

标签: ios objective-c uiimageview frame cgrect


【解决方案1】:

首先:你需要为 imageView 设置 contentMode

imageView.contentMode = UIViewContentModeScaleAspectFill;

第二:按视图边界设置 imageFrame 帧

imageView.frame = self.view.bounds;

第三:你使用自动布局

【讨论】:

  • 我将内容模式设置为 AspectModeFit。所以这两个都解决了这个问题。非常感谢您的快速回复!
【解决方案2】:

有几点需要考虑:

  • 您确定您的 image/imageView contentMode 设置正确吗?

    imageView.contentMode = UIViewContentModeScaleAspectFill;

  • 您确定已正确设置了自动布局约束/自动调整大小掩码吗?

  • 您是否注意过屏幕旋转?

如果您发布一些代码示例会很有帮助。否则,很难准确找到问题所在。

希望这会有所帮助。

【讨论】:

    【解决方案3】:
    imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    

    imageView.frame = self.view.bounds;
    

    还可以在情节提要或代码中设置自动调整大小掩码 在代码中:

    imageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | 
    UIViewAutoresizingFlexibleTopMargin | 
    UIViewAutoresizingFlexibleRightMargin | 
    UIViewAutoresizingFlexibleLeftMargin | 
    UIViewAutoresizingFlexibleHeight | 
    UIViewAutoresizingFlexibleWidth;
    

    如果您使用自动布局,请查看此链接Matching subview's width to it's superview using autolayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多