XCODE:从 iPhone 5 设备的 NIB 调整 UIImageView 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14116088/
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
XCODE: Adjusting UIImageView Size from NIB for iPhone 5 Devices
提问by JRoss
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.
我正在尝试为用户调整背景静态UIImageView
(来自Nib
文件)的大小iPhone5
。不幸的是,以下代码似乎对背景视图的大小没有任何影响。
Does anyone know why? Thanks in advance for any help you can provide.
有谁知道为什么?预先感谢您提供的任何帮助。
ViewController.m:
视图控制器.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
回答by Rob
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.);
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 = 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:
正如 demosten 和 shabzco 所建议的,我不会使用设备名称/描述来确定坐标(如果没有别的,iPhone 6 等等);
如果您打算以
frame
编程方式设置,我建议background.frame
根据视图控制器的视图设置属性,bounds
而不是硬编码图像视图的大小。这样,背景会被调整为视图控制器视图的适当大小,不仅与设备无关,而且无论该视图控制器在以后是否嵌入为另一个容器控制器的子视图控制器等。 (例如,如果您将视图放在导航控制器和标签栏控制器、您自己的自定义容器控制器等中会怎样)。另外,不要假设状态栏或其他图形元素的大小。让 iOS 为您解决所有这些问题:background.frame = self.view.bounds;
或者更好的是,如果您已将背景图像视图添加到 NIB 本身,请设置自动调整大小的掩码并且根本不要以
frame
编程方式更改。如果您关闭了自动布局,只需像这样设置图像视图的自动调整大小属性:
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、将您的视图控制器嵌入其他容器控制器等问题的影响,并限制您的代码的重用机会。
回答by shabbirv
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)];
}
}
回答by demosten
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 在“大小”检查器选项卡中没有灵活的宽度或高度。