iOS屏幕适配(只支持竖屏)

接手公司项目,在里面看见了屏幕适配的方法,感觉很方便,缺点就是只支持竖屏。

1.首先在工程里面创建一个pch文件。这个百度一下就行,网址//www.greatytc.com/p/67ce72c4ad6c
2.再来到appDelegate.h里面生成两个属性。

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (assign, nonatomic) float autoSizeScaleX;
@property (assign, nonatomic) float autoSizeScaleY;

@end

3.来到appDelegate.m里面,先将获得屏幕的宽高写个宏定义,再在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里面添加下面代码:

#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 {
    // Override point for customization after application launch.
    
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    
    if(ScreenHeight > 480 && ScreenHeight<1024){
        myDelegate.autoSizeScaleX = ScreenWidth/320;
        myDelegate.autoSizeScaleY = ScreenHeight/568;
    }else if(ScreenHeight >= 1024){
        myDelegate.autoSizeScaleX = ScreenWidth/320;
        myDelegate.autoSizeScaleY = ScreenHeight*1.333333/568;
    }else
    {
        myDelegate.autoSizeScaleX = 1.0;
        myDelegate.autoSizeScaleY = 1.0;
    }

    
    
    return YES;
}

4.在pch文件里面添加AppDelegate的头文件

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "AppDelegate.h"

#endif /* PrefixHeader_pch */

5.创建一个Header file文件如下图

424948DD-28F6-4701-B2FB-EB5BC57D2FDD.png

6.在Header file文件里面添加下面代码

#ifndef SetFrame_h
#define SetFrame_h
CG_INLINE CGRect CGRectMakes(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;
    return rect;
}

CG_INLINE CGSize CGSizeMakes(CGFloat width, CGFloat height)
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGSize rect;
    rect.width  = width * myDelegate.autoSizeScaleX;
    rect.height = height * myDelegate.autoSizeScaleY;
    return rect;
}

CG_INLINE CGPoint CGPointMakes(CGFloat width, CGFloat height){
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGPoint rect;
    rect.x  = width * myDelegate.autoSizeScaleX;
    rect.y = height * myDelegate.autoSizeScaleY;
    return rect;
}

CG_INLINE CGFloat TableCellHeight(CGFloat height){
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGFloat newHeight;
    newHeight  = height * myDelegate.autoSizeScaleY;
    return newHeight;
}


#endif /* SetFrame_h */

7.在pch文件里面添加Header file的头文件

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "AppDelegate.h"
#import "SetFrame.h"

#endif /* PrefixHeader_pch */

这样就可以使用了,有4个方法可以调用,分别是CGRectMakes(CGFloat x, CGFloat y, CGFloat width, CGFloat height)、CGSizeMakes(CGFloat width, CGFloat height)、CGPointMakes(CGFloat width, CGFloat height)和TableCellHeight(CGFloat height)。使用的时候直接将系统的修改成这是个方法,比如创建一个label,直接将系统的CGRectMake改为CGRectMakes就可以了。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMakes(10, 10,10 , 10)];
    label.backgroundColor = [UIColor redColor];
    [self.view addSubview:label];      
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.NSTimer //暂停if ([timer isValid]) {[timer setFireDate:[N...
    俊月阅读 1,373评论 0 0
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,721评论 2 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,542评论 1 14
  • 《这样读书就够了》已经预热了很久,这几天陆陆续续很多人分享笔记,这本书对学习者进行了划分,高级学习者的一个核心关键...
    小铮古阅读 251评论 0 0