iOS 启动页添加版本号

XCode中有LaunchScreen.storyboard或者LaunchScreen.xib,苹果默认使用的就是拿这个当启动页,这是一个静态的页面,也就是只能用自动布局来适配屏幕的大小,就一张图片居中显示,这个兼容性不强。另外一种方法,这种方法很多人都在用,只要设置正确 尺寸格式正确就可以正常显示的。常用的尺寸格式如下:

iOS 启动页添加版本号_第1张图片
CA87F4BA-7F54-486B-9E8D-6B5AB1190D26.png
iOS 启动页添加版本号_第2张图片
B1A951D8-A8A2-43AB-B8F0-2EC86F61DCEA.png
步骤如下:

1、点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image"
2、如图,右侧的勾选可以让你选择是否要对ipad,横屏,竖屏,以及低版本的ios系统做支持.这边我选了ios8.0,ios7.0,ios6没有做支持.

iOS 启动页添加版本号_第3张图片
55A47D57-7A6A-4DBD-91AD-B89C63CB2A8A.png

3、选择launchImage


iOS 启动页添加版本号_第4张图片
975202FB-6791-4DD8-B1B0-521401F43097.png

4、清空Launch Screen File


iOS 启动页添加版本号_第5张图片
72CB1773-D0F9-4466-814D-91DC7FA24454.png

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;
}

运行结果:

iOS 启动页添加版本号_第6张图片
3B27A049-EA3F-4F0E-A78D-B066EB5FDD9F.png

你可能感兴趣的:(iOS 启动页添加版本号)