xcode 如何根据 iPhone 屏幕的大小更改图像?请帮助我
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15372256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change an image based on the size of the iphone screen? Please assist me
提问by Andy A
On the front page of my app I have two images, which are laid over a portion of two buttons. The buttons are automatically re-scaled (via depending on the size of the iPhone screen. Unfortunately when the images are re-scaled it ends up being pretty ugly.
在我的应用程序的首页上,我有两个图像,它们位于两个按钮的一部分上。按钮会自动重新缩放(通过取决于 iPhone 屏幕的大小。不幸的是,当图像重新缩放时,它最终变得非常难看。
Can I dictate the size and location of the two images (via code), depending on the size of the iphone screen? If so how would I go about doing so?
我可以根据 iphone 屏幕的大小指定两个图像的大小和位置(通过代码)吗?如果是这样,我将如何去做?
Thanks for helping a novice out!
感谢您帮助新手!
Update 13MAR13
2013 年 3 月 13 日更新
This is the code I tried before. It causes no errors, but no images appear on the page!
这是我之前试过的代码。它不会导致任何错误,但页面上不会出现图像!
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
UIImageView *myImageView = [[UIImageView alloc]initWithFrame: CGRectMake(220, 2.5, 95, 75)];
myImageView.image = [UIImage imageNamed:@"BFS.png"];
UIImageView *myImageViewtwo = [[UIImageView alloc]initWithFrame: CGRectMake(300, 100, 95, 75)];
myImageViewtwo.image = [UIImage imageNamed:@"RMS.png"];
}
if(result.height == 568)
{
UIImageView *myImageView = [[UIImageView alloc]initWithFrame: CGRectMake(100, 400, 95, 75)];
myImageView.image = [UIImage imageNamed:@"BFS.png"];
UIImageView *myImageViewtwo = [[UIImageView alloc]initWithFrame: CGRectMake(100, 300, 95, 75)];
myImageViewtwo.image = [UIImage imageNamed:@"RMS.png"];
}
}
回答by MS.
You can take 2 different imags & UIImageViews behind transparent buttons and set their sizes according to screen size.
您可以在透明按钮后面使用 2 个不同的图像和 UIImageViews,并根据屏幕大小设置它们的大小。
Code for get screen size:
获取屏幕尺寸的代码:
int h = [[UIScreen mainScreen] bounds].size.height;
Then, check
然后,检查
if([[UIScreen mainScreen] bounds].size.height == 480) {
imgVw1.frame = CGRectMake(x, y, w, h);
imgVw1.image = [UIImage imagenamed:@"....png"];
} //iPhone-4
if([[UIScreen mainScreen] bounds].size.height == 568) {
imgVw1.frame = CGRectMake(x, y, w, h);
imgVw1.image = [UIImage imagenamed:@"[email protected]"];
} //iPhone-5
You can take two images for it.
您可以为它拍摄两张图像。
- imgButton.png -> Normal
- [email protected] -> Retina Display
- imgButton.png ->正常
- [email protected] -> Retina 显示
Edited Code编辑代码
In .h file,
在 .h 文件中,
@property(nonatomic, retain) IBOutlet UIButton *btn;
In. m file,
在。m文件,
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[btn setFrame:CGRectMake(75, 20, 175, 100)];
[btn setImage:[UIImage imageNamed:@"Img_Circle.png"] forState:UIControlStateNormal];
}
if(result.height == 568)
{
[btn setFrame:CGRectMake(65, 60, 200, 120)];
[btn setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];
}
}
As we already declared property in .h file, then no need to alloc again here.
Just simply set frames for it.
由于我们已经在 .h 文件中声明了属性,所以这里不需要再次分配。
只需为它设置框架即可。
Hopefully, this will help to you.
Thanks.
希望这会对您有所帮助。
谢谢。