使用self.view.frame时,UIImageView initWithFrame的大小不正确

时间:2014-05-12 01:20:58

标签: ios objective-c uiimageview frame cgrect

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

提前感谢您的帮助。

ImageViews as they appear in the simulator

3 个答案:

答案 0 :(得分:1)

首先:您需要为imageView设置contentMode

imageView.contentMode = UIViewContentModeScaleAspectFill;

第二:按视图边界设置imageFrame框架

imageView.frame = self.view.bounds;

第三:使用自动布局

答案 1 :(得分:0)

有一些事情需要考虑:

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

    imageView.contentMode = UIViewContentModeScaleAspectFill;

  • 您确定自动布局约束/自动调整遮罩是否设置正确?

  • 您是否负责过屏幕旋转?

如果您发布一些代码示例,它会很有帮助。否则,很难找到问题的确切位置。

希望这有帮助。

答案 2 :(得分:0)

imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];

imageView.frame = self.view.bounds;

还可以在故事板或代码中设置自动调整大小 在代码中:

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

enter image description here

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