XCode中有LaunchScreen.storyboard或者LaunchScreen.xib,苹果默认使用的就是拿这个当启动页,这是一个静态的页面,也就是只能用自动布局来适配屏幕的大小,就一张图片居中显示,这个兼容性不强。另外一种方法,这种方法很多人都在用,只要设置正确 尺寸格式正确就可以正常显示的。常用的尺寸格式如下:
步骤如下:
1、点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image"
2、如图,右侧的勾选可以让你选择是否要对ipad,横屏,竖屏,以及低版本的ios系统做支持.这边我选了ios8.0,ios7.0,ios6没有做支持.
3、选择launchImage
4、清空Launch Screen File
5、加入版本号的代码
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
[self customLaunchImageView];
return YES;
}
- (void)customLaunchImageView
{
UIImageView *launchImageView = [[UIImageView alloc] initWithFrame:self.window.bounds];
launchImageView.image = [self getLaunchImage];
[self.window addSubview:launchImageView];
[self.window bringSubviewToFront:launchImageView];
UILabel *vesionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 260, 100, 30)];
vesionLabel.backgroundColor = [UIColor cyanColor];
//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
vesionLabel.text = [NSString stringWithFormat:@"V %@",currentVersion];
vesionLabel.textAlignment = NSTextAlignmentCenter;
[launchImageView addSubview:vesionLabel];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.2 animations:^{
launchImageView.alpha = 0.0;
launchImageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
[launchImageView removeFromSuperview];
}];
});
}
- (UIImage *)getLaunchImage
{
UIImage *lauchImage = nil;
NSString *viewOrientation = nil;
CGSize viewSize = [UIScreen mainScreen].bounds.size;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
viewOrientation = @"Landscape";
} else {
viewOrientation = @"Portrait";
}
NSArray *imagesDictionary = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary *dict in imagesDictionary) {
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
lauchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
}
}
return lauchImage;
}
运行结果: