您的位置:首页 > 移动开发 > IOS开发

iOS中的屏幕适配

2016-04-28 00:00 423 查看
iOS中的屏幕适配

在AppDelegate.h文件中

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

//屏幕的尺寸
@property float autoSizeScaleX;
@property float autoSizeScaleY;

@property (strong, nonatomic) UIWindow *window;

@end


在AppDelegate.m文件中

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "FMDatabase.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface AppDelegate ()
{

//创建一个导航控制器
UINavigationController *nav;
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//按比例适配屏幕
AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if(ScreenWidth == 667){

myDelegate.autoSizeScaleX = 1.0;
myDelegate.autoSizeScaleY = 1.0;
}
else{
myDelegate.autoSizeScaleX = ScreenWidth/375;
myDelegate.autoSizeScaleY = ScreenHeight/667;
}

//让当前的window成为主窗口
[self.window makeKeyAndVisible];

//首页(可以写登录页面)
HomeViewController *homeVC = [[HomeViewController alloc]init];

//创建一个导航控制器
nav= [[UINavigationController alloc]initWithRootViewController:homeVC];
self.window.rootViewController = nav;


设置在4S、5、5S、6、6P、6S、6SP等不同尺寸屏幕上的适配
//设置界面上的画面
UIImageView * showView= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds ].size.width, [[UIScreen mainScreen]bounds ].size.height)];
showView.image = [UIImage imageNamed:@"新建View"];
[self.view addSubview:showView];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: