0

I am merging some code+xib layouts from an old iOS app into a current app.

All is working apart from layouts on iPhone 5/6. The original app does expand to fit those screens. However, I'm only using a subset of the view controllers from the old app, launched from a new button in the current app.

What I've been seeing is the iPhone 4 size layout occupying the top left of the screen on e.g. iPhone 6, when the view controller from the old code is loaded.

I've tried many different things. The one thing that made a slight difference was looking at the code for the old app's SplashScreen:

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {

    //Init the View
    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *view = [[UIView alloc] initWithFrame:appFrame];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
    self.view = view;
    [view release];

    NSString *splashImageName = nil;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        splashImageName = @"iPadDefault2.png";
    else
        splashImageName = @"Default.png";

    mSplashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:splashImageName]];
    [self.view addSubview:mSplashImageView];

}

So I added the following to the HomeScreenViewController which is the view launched from the new app:

- (void)loadView
{   
    [[[NSBundle mainBundle] loadNibNamed:@"HomeScreenViewController" owner:self options:nil] objectAtIndex:0];
    self.view.autoresizesSubviews = YES;
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    UIImage* backgroundImage = [UIImage imageNamed:@"Default-no-logo.png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

    [self.view setFrame:appFrame];
}

Now I get the background image tiled to fill the space on the iPhone 6, however the interface is still in the same place and size in the top left.

I've looked at answers such as UIView added programmatically full screen wrong size, creating a custom UIView, but it does not seem to help my situation.

EDIT:

If I change the loadView method to:

NSString *splashImageName = nil;

splashImageName = @"Default-no-logo.png";

mSplashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:splashImageName]];

mSplashImageView.contentMode = UIViewContentModeScaleAspectFit;

[mSplashImageView setFrame:appFrame];

[self.view addSubview:mSplashImageView];

I get the image show full screen. However, doing this with a custom UIView does not work. How can I get the UIView to respect the autoresizingMask and grow with a larger display?

I've set debug points at various places and, for iPhone 6, it always says the height is 667.

Community
  • 1
  • 1
Adamski
  • 3,585
  • 5
  • 42
  • 78

1 Answers1

0

This should not be the answer, so I will not mark it as such, but this is the workaround I came up with that actually works in this situation (I spent almost 2 days trying many different ways).

Basically I'm using an AffineTransform to enlarge the view to fill the screen, keeping its aspect ratio.

AspectFillViewController.h:

#import <UIKit/UIKit.h>
@interface AspectFillViewController : UIViewController
@end

#import "AspectFillViewController.h"
@interface AspectFillViewController ()
@end

AspectFillViewController.m:

@implementation AspectFillViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

    self.view.frame = appFrame;

    // Default size: 320 x 460 - this is the size of the xib layout
    int defaultWidth = 320;
    int defaultHeight = 460;

    // Hacky resizing code
    if (appFrame.size.width > defaultWidth || appFrame.size.height > defaultHeight)
    {
        CGFloat scaleX = appFrame.size.width / defaultWidth;
        CGFloat scaleY = appFrame.size.height / defaultHeight;
        CGFloat scale = scaleY > scaleX ? scaleX : scaleY;
        self.view.transform = CGAffineTransformMakeScale(scale, scale);

        // This does not work (hence not an ideal solution)
        self.view.center = CGPointMake(appFrame.size.width/2, appFrame.size.height/2); 

    } 
}
Adamski
  • 3,585
  • 5
  • 42
  • 78