我正在将旧iOS应用中的一些代码+ xib布局合并到当前应用中。
所有功能都与iPhone 5/6上的布局不同。原始应用程序确实扩展到适合这些屏幕。但是,我只使用旧应用程序中的视图控制器的子集,从当前应用程序中的新按钮启动。
我所看到的是iPhone 4尺寸布局占据屏幕的左上角,例如iPhone 6,当加载旧代码的视图控制器时。
我尝试过很多不同的事情。稍微有点不同的是查看旧应用程序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];
}
所以我将以下内容添加到HomeScreenViewController
,这是从新应用启动的视图:
- (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];
}
现在我将背景图片平铺以填充iPhone 6上的空间,但界面仍然位于左上角的相同位置和大小。
我已经查看了UIView added programmatically full screen wrong size等答案,创建了自定义UIView,但它似乎对我的情况没有帮助。
修改
如果我将loadView
方法更改为:
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];
我让图片全屏显示。但是,使用自定义UIView执行此操作不起作用。如何让UIView尊重autoresizingMask
并以更大的显示增长?
我已经在各个地方设置了调试点,对于iPhone 6,它总是说高度为667.
答案 0 :(得分:0)
这不应该是答案,所以我不会这样做,但这是我提出的解决方法,实际上在这种情况下工作(我花了差不多2天尝试了很多不同的方式)。
基本上我使用AffineTransform放大视图以填充屏幕,保持其纵横比。
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);
}
}