简体   繁体   English

iphone4中图像未全屏显示

[英]image not showing full screen in iphone4

I have an image at the center of the view in Storyboard. 我在情节提要中的视图中心有一个图像。 On iphone 5 it shows fullscreen, but on iphone 4 (I use a different image on iphone 4 - set programmatically), it shows white space at the top. 在iPhone 5上,它显示为全屏显示,但在iPhone 4(我在iPhone 4上使用其他图像-通过程序设置)上,则在顶部显示空白。

How do I remove the white space at the top on iphone 4? 如何删除iPhone 4顶部的空白?

Here's the code: 这是代码:

UIImage * img;

if (IS_IPHONE_5) {
    img = [UIImage imageNamed: @"page1"];
} else {
    img = [UIImage imageNamed: @"ip4_page1"];
}

[self.centerImage setImage: img];
[self.centerImage setFrame: self.view.frame];

It's because of the screen aspect ratio. 这是因为屏幕的宽高比。 You can change the UIImageView way of displaying an image like this: 您可以更改UIImageView显示图像的方式,如下所示:

self.yourImage.contentMode = UIViewContentModeScaleAspectFill;

That way the image will expand maintaining its aspect. 这样,图像将保持其外观扩展。 You can try with other configurations to achieve different results. 您可以尝试其他配置以获得不同的结果。 Cheers 干杯

Assuming the image is indeed filling your whole screen (which you can test by giving it a background color), you need to make sure that the fill mode is correct. 假设图像确实充满了整个屏幕(可以通过提供背景色来进行测试),则需要确保填充模式正确。

self.centerImage.contentMode = UIViewContentModeScaleToFill
// or
self.centerImage.contentMode = UIViewContentModeScaleAspectFill

The first option will show all of the image, so it might be distorted. 第一个选项将显示所有图像,因此可能会变形。
The second option will avoid distortion, but might cut off top/bottom or left/right edges. 第二个选项将避免变形,但可能会切掉顶部/底部或左侧/右侧边缘。

Set your UIImageView 's contentMode to UIViewContentModeScaleAspectFill . 将您的UIImageViewcontentModeUIViewContentModeScaleAspectFill

self.centerImage.contentMode = UIViewContentModeScaleAspectFill;

You could also set your UIImageView 's background color to clear if increasing the images size to fill the view does not meet your needs. 如果增加图像大小以填充视图不满足您的需要,还可以将UIImageView的背景色设置为clear

self.centerImage.backgroundColor = [UIColor clearColor];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM