I am trying to adjust the size of a background static UIImageView
(from Nib
file) for iPhone5
users. Unfortunately, the following code does not seem to make any difference on the background view's size.
我正在尝试为iPhone5用户调整后台静态UIImageView(来自Nib文件)的大小。不幸的是,以下代码似乎没有对背景视图的大小产生任何影响。
Does anyone know why? Thanks in advance for any help you can provide.
有谁知道为什么?提前感谢您提供的任何帮助。
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
device = appDelegate.deviceType;
NSLog(@"The device platform is: %@", device);
if ([[device substringToIndex:8] caseInsensitiveCompare: @"iPhone 5"] == NSOrderedSame) {
[background sizeThatFits:CGSizeMake(320, 504)];
}
else {
[background sizeThatFits:CGSizeMake(320, 416)];
}
...
//Note: 'background' is declared in `ViewController.h` as, `IBOutlet` `UIImageView` *background, and is linked to the image view in ViewController_iPhone.xib
3 个解决方案
#1
4
A few thoughts:
一些想法:
-
As demosten and shabzco suggested, I wouldn't use a device name/description to determine coordinates (if nothing else, what about the iPhone 6, etc.);
正如demosten和shabzco建议的那样,我不会使用设备名称/描述来确定坐标(如果没有别的,那么iPhone 6等等);
-
If you're going to set the
frame
programmatically, I would suggest setting thebackground.frame
property based upon the view controller's view'sbounds
rather than hard coding the size of the image view. That way, the background is adjusted to the appropriate size of the view controller's view, not only regardless of device, but also regardless if that view controller is, at a later date, embedded as a child view controller of another container controller, etc. (E.g., what if you put your view in a navigation controller and tab bar controller, your own custom container controller, etc.). Also, don't make assumptions about the size of the status bar or other graphical elements. Let iOS figure all of this out for you with simply:如果您要以编程方式设置框架,我建议根据视图控制器的视图边界设置background.frame属性,而不是硬编码图像视图的大小。这样,背景被调整到视图控制器视图的适当大小,不仅与设备无关,而且无论该视图控制器是否在以后被嵌入为另一个容器控制器的子视图控制器等。 (例如,如果您将视图放在导航控制器和标签栏控制器,您自己的自定义容器控制器等中,该怎么办)。此外,不要假设状态栏或其他图形元素的大小。让iOS简单地为您解决所有这些问题:
background.frame = self.view.bounds;
-
Or better yet, if you've added the background image view to the NIB itself, set the autosizing mask and don't change the
frame
programmatically at all. If you have autolayout turned off, just set the autosizing properties of your image view like so:或者更好的是,如果您已将背景图像视图添加到NIB本身,请设置自动调整遮罩,并且不要以编程方式更改框架。如果您关闭了自动布局,只需设置图像视图的自动调整属性,如下所示:
Bottom line, if you can, avoid explicit device name references in your code and avoid hard coded coordinates. To have hardcoded dimensions in your code will just make your app more fragile, susceptible to problems with new devices, new versions of iOS, embedding your view controller in additional container controllers, etc., and limits the reuse opportunities for your code.
如果可以,可以在底线中避免代码中显式的设备名称引用,并避免硬编码坐标。在代码中使用硬编码维度只会使您的应用程序更加脆弱,容易受到新设备,iOS新版本,将视图控制器嵌入其他容器控制器等问题的影响,并限制代码的重用机会。
#2
2
Here's a better way to check between iPhone 5 and previous sized devices
这是在iPhone 5和以前尺寸的设备之间进行检查的更好方法
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[background sizeThatFits:CGSizeMake(320, 416)];
}
if(result.height == 568)
{
[background sizeThatFits:CGSizeMake(320, 504)];
}
}
#3
1
sizeThatFits
does not change size. You should modify background.frame
. Something like:
sizeThatFits不会改变大小。你应该修改background.frame。就像是:
background.frame = CGRectMake(background.frame.origin.x, background.frame.origin.y, 320, 416);
and
background.frame = CGRectMake(background.frame.origin.x, background.frame.origin.y, 320, 504);
Also make sure your UIImageView does not have flexible width or height in Size inspector tab while editing your Nib file.
在编辑Nib文件时,还要确保UIImageView在“大小检查器”选项卡中没有灵活的宽度或高度。
#1
4
A few thoughts:
一些想法:
-
As demosten and shabzco suggested, I wouldn't use a device name/description to determine coordinates (if nothing else, what about the iPhone 6, etc.);
正如demosten和shabzco建议的那样,我不会使用设备名称/描述来确定坐标(如果没有别的,那么iPhone 6等等);
-
If you're going to set the
frame
programmatically, I would suggest setting thebackground.frame
property based upon the view controller's view'sbounds
rather than hard coding the size of the image view. That way, the background is adjusted to the appropriate size of the view controller's view, not only regardless of device, but also regardless if that view controller is, at a later date, embedded as a child view controller of another container controller, etc. (E.g., what if you put your view in a navigation controller and tab bar controller, your own custom container controller, etc.). Also, don't make assumptions about the size of the status bar or other graphical elements. Let iOS figure all of this out for you with simply:如果您要以编程方式设置框架,我建议根据视图控制器的视图边界设置background.frame属性,而不是硬编码图像视图的大小。这样,背景被调整到视图控制器视图的适当大小,不仅与设备无关,而且无论该视图控制器是否在以后被嵌入为另一个容器控制器的子视图控制器等。 (例如,如果您将视图放在导航控制器和标签栏控制器,您自己的自定义容器控制器等中,该怎么办)。此外,不要假设状态栏或其他图形元素的大小。让iOS简单地为您解决所有这些问题:
background.frame = self.view.bounds;
-
Or better yet, if you've added the background image view to the NIB itself, set the autosizing mask and don't change the
frame
programmatically at all. If you have autolayout turned off, just set the autosizing properties of your image view like so:或者更好的是,如果您已将背景图像视图添加到NIB本身,请设置自动调整遮罩,并且不要以编程方式更改框架。如果您关闭了自动布局,只需设置图像视图的自动调整属性,如下所示:
Bottom line, if you can, avoid explicit device name references in your code and avoid hard coded coordinates. To have hardcoded dimensions in your code will just make your app more fragile, susceptible to problems with new devices, new versions of iOS, embedding your view controller in additional container controllers, etc., and limits the reuse opportunities for your code.
如果可以,可以在底线中避免代码中显式的设备名称引用,并避免硬编码坐标。在代码中使用硬编码维度只会使您的应用程序更加脆弱,容易受到新设备,iOS新版本,将视图控制器嵌入其他容器控制器等问题的影响,并限制代码的重用机会。
#2
2
Here's a better way to check between iPhone 5 and previous sized devices
这是在iPhone 5和以前尺寸的设备之间进行检查的更好方法
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[background sizeThatFits:CGSizeMake(320, 416)];
}
if(result.height == 568)
{
[background sizeThatFits:CGSizeMake(320, 504)];
}
}
#3
1
sizeThatFits
does not change size. You should modify background.frame
. Something like:
sizeThatFits不会改变大小。你应该修改background.frame。就像是:
background.frame = CGRectMake(background.frame.origin.x, background.frame.origin.y, 320, 416);
and
background.frame = CGRectMake(background.frame.origin.x, background.frame.origin.y, 320, 504);
Also make sure your UIImageView does not have flexible width or height in Size inspector tab while editing your Nib file.
在编辑Nib文件时,还要确保UIImageView在“大小检查器”选项卡中没有灵活的宽度或高度。