屏幕适配

AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property float autoSizeScaleX;
@property float autoSizeScaleY;
@end

AppDelegate.m

#import "AppDelegate.h"
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface AppDelegate ()

@end

@implementation AppDelegate


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

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    if (ScreenHeight > 568 || ScreenHeight < 568) {
        myDelegate.autoSizeScaleX = ScreenWidth / 320;
        myDelegate.autoSizeScaleY = ScreenWidth / 568;
    } else {
        myDelegate.autoSizeScaleX = 1.0;
        myDelegate.autoSizeScaleY = 1.0;
    }
    return YES;
}

ViewController.m

#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake1(100, 100, 50, 50)];

    imageview.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageview];
}

CG_INLINE CGRect  CGRectMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGRect rect;

    rect.origin.x = x * myDelegate.autoSizeScaleX;
    rect.origin.y = y * myDelegate.autoSizeScaleY;
    rect.size.width = width * myDelegate.autoSizeScaleX;
    rect.size.height = height * myDelegate.autoSizeScaleY;

    if (ScreenHeight == 480) {
        rect.origin.x = x * myDelegate.autoSizeScaleY;
        rect.origin.y = y * myDelegate.autoSizeScaleY;
        rect.size.width = width * myDelegate.autoSizeScaleY;
        rect.size.height = height * myDelegate.autoSizeScaleY;
    }
    return rect;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tongwei117/article/details/51558037
文章标签: 屏幕自动适配
个人分类: ios开发
上一篇比较两个日期的大小
下一篇使用AFN多张图片上传
想对作者说点什么? 我来说一句

Android 屏幕适配工具

2018年05月29日 20KB 下载

屏幕自动适配demo

2016年01月26日 1.33MB 下载

android px适配

2015年11月04日 95KB 下载

没有更多推荐了,返回首页

关闭
关闭