关闭

屏幕适配

标签: 屏幕自动适配
128人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44933次
    • 积分:1973
    • 等级:
    • 排名:第19846名
    • 原创:162篇
    • 转载:6篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论