set background images for Iphone 3g, Iphone 4s and

2019-02-21 03:12发布

I want to set the background image of a view. I use three images: backgroundimage.png (size 320 x 480), backgroundimage@2x.png (size 640 x 960) and backgroundimage-568h@2x.png (size 640 x 1136)

that works for iphone 3g size. But when I test it for retina size the background image is just a small part of the original image. I think, that I have to downscale the image somehow, or set a frame for it, but I just don't know how.

Thanks for any help ;)

thats the code:

self.view.backgroundColor = [UIColor clearColor];
UIImage *backgroundImage = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
    backgroundImage = [UIImage imageNamed:@"background-568h@2x"];
}
else{
    backgroundImage = [UIImage imageNamed:@"background"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];

1条回答
The star\"
2楼-- · 2019-02-21 03:49

My first guess is because of the misuse of colorWithPatternImage. This should only be used if you have a pattern image that you wish to have tiled. Instead try creating a UIImageView the size of the view, and then adding it as a subview of self.view.

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
查看更多
登录 后发表回答