Xcode: Adjust UIImageView size for iPhone 5 devices

I am trying to adjust the size of the background static UI ImageView (from the Nib file) for iPhone5 users. Unfortunately, the following code does not seem to have any effect on the size of the background view.

< /p>

Does anyone know why? Thank you in advance for any help.

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)];
}
...< br />//Note:'background' is declared in `ViewController.h` as, `IBOutlet` `UIImageView` *background, and is linked to the image view in ViewController_iPhone.xib

Some thoughts:

>As suggested by demosten and shabzco, I will not use the device name/description To determine the coordinates (if nothing else, then iPhone 6, etc.);
>If you want to set the frame programmatically, I recommend setting the back according to the view boundary of the view controller The ground.frame property, instead of hard-coding the size of the image view. In this way, the background is adjusted to the appropriate size of the view controller view, not only has nothing to do with the device, but also regardless of whether the view controller is embedded as another container controller in the future (For example, what if you put the view in the navigation controller and tab bar controller, your own custom container controller, etc.). Also, don’t assume the status bar or other graphics The size of the element. Let iOS simply do all this for you:

background.frame = self.view.bounds;

> or better Yes, if you have added the background image view to the NIB itself, please set the auto-adjustment mask, and do not change the frame programmatically. If you turn off autolayout, just set the auto-adjustment properties of the image view as follows: /p>

If you can, you can avoid explicit device name references in the code in the bottom line, and avoid hard-coded coordinates. Using hard-coded dimensions in your code will only make your application more vulnerable and vulnerable to new devices , the new version of iOS, embedding view controllers into other container controllers and other issues, and restricting code reuse opportunities.

I am trying to adjust the background static state for iPhone5 users The size of the UI ImageView (from the Nib file). Unfortunately, the following code does not seem to have any effect on the size of the background view.

Does anyone know why? Thank you in advance for any help.

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)];
}
...< br />//Note:'background' is declared in `ViewController.h` as, `IBOutlet` `UIImageView` *background, and is linked to the image view in ViewController_iPhone.xib

Some thoughts:

>As suggested by demosten and shabzco, I will not use the device name/description to determine the coordinates (if nothing else, then iPhone 6 Etc.);
>If you want to set the frame programmatically, I recommend setting the background.frame property based on the view boundary of the view controller instead of hard-coding the size of the image view. In this way, the background is adjusted to the view controller The appropriate size of the view is not only related to the design It is irrelevant, and regardless of whether the view controller is embedded as a child view controller of another container controller in the future. (For example, if you put the view in the navigation controller and the tab bar controller, your own custom In container controllers, etc., what to do). Also, don’t assume the size of the status bar or other graphic elements. Let iOS simply do all this for you:

background. frame = self.view.bounds;

> Or better yet, if you have added the background image view to the NIB itself, please set an auto-adjustment mask and do not change the frame programmatically. If You turn off autolayout, just set the automatic adjustment properties of the image view, as shown below:

If you can, you can avoid clear device name references in the code in the bottom line, and avoid using hard-coded coordinates. The use of hard-coded dimensions in the code will only make your application more fragile, vulnerable to issues such as new devices, new versions of iOS, embedding view controllers into other container controllers, and limit code reuse opportunities.

Leave a Comment

Your email address will not be published.