zoukankan      html  css  js  c++  java
  • iOS屏幕适配(尺寸适配)

    屏幕尺寸适配:一

    在.pch中加入以下代码,在定义每个尺寸值的时候都调用下边的宏

    //以iphone7为例  定义 view相关的宽高宏
    #define
    IPHONEHIGHT(b) [UIScreen mainScreen].bounds.size.height*((b)/1334.0) #define IPHONEWIDTH(a) [UIScreen mainScreen].bounds.size.width*((a)/750.0) //有导航栏和分栏的情况下高度 #define PhoneHight(d) ([UIScreen mainScreen].bounds.size.height-113.0)*((d)/1108.0) //仅仅有导航栏的情况下高度 #define PHONEHIGHT(d) ([UIScreen mainScreen].bounds.size.height-64.0)*((d)/1206.0) #define ScreenWidth CGRectGetWidth([UIScreen mainScreen].bounds) #define ScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds)

     屏幕尺寸适配:二

    创建项目-创建PCH文件:若要创建.pch , 在other里选择 PCH file,并需要修改一下设置。在build settings 里设置 Precompile Prefix Header的值为YES,并设置Prefix Header的路径。

    创建一个扩展文件UIView+CreaFream.h,(如何创建扩展文件:创建一个objective-c file , 可以选择 category, extension ,protocol, empty 文件。选category 就能建立类别。)再不会了看网址 http://blog.csdn.net/idoshi201109/article/details/51735461

    擦,差点忘了 PCH里边的东西了

    .pch

    #ifndef PrefixHeader_pch
    #define PrefixHeader_pch
    
    //判断ios版本
    #define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0 )
    
    
    #import "UIView+CreaFream.h"
    
    #define IPHONE4 ScreenRect.size.width ==320 && ScreenRect.size.height ==480
    
    #define IPHONE5 ScreenRect.size.width ==320 && ScreenRect.size.height ==568
    
    #define IPHONE6 ScreenRect.size.width ==375 && ScreenRect.size.height ==667
    
    #define IPHONE6p ScreenRect.size.width ==414 && ScreenRect.size.height ==736
    
    //#define iPhone7 iPhone7P 自己补充,不过和6的尺寸一样的
    
    
    #define MAS_SHORTHAND
    #define MAS_SHORTHAND_GLOBALS
    
    
    
    
    #endif /* PrefixHeader_pch */

    .h

    #import <UIKit/UIKit.h>
    
    @interface UIView (CreaFream)
    
    //只需一个设置间隔 大小的方法
    +(CGRect)GetRectWithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)W Heigth:(CGFloat)H;
    
    @end
    

      

    .m

    #import "UIView+CreaFream.h"
    
    @implementation UIView (CreaFream)
    
    //实现方法
    +(CGRect)GetRectWithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)W Heigth:(CGFloat)H{
        
        CGRect temptect = CGRectZero;
       
    //个人喜欢以iPhone 为基准 进行比例大小的对比
    
        if (IPHONE6) {
            temptect = CGRectMake(x,y, W, H);
            
        }else if ((IPHONE4)||(IPHONE5)){
            
            CGFloat Xscole = 320/375.0;
            CGFloat Yscole = 480/667.0;
            
            temptect = CGRectMake(x*Xscole, y*Yscole, W*Xscole, Yscole*H);
            
            
        }else if (IPHONE6p){
            
            CGFloat Xscole = 414/375.0;
            CGFloat Yscole = 736/667.0;
            
            temptect = CGRectMake(x*Xscole, y*Yscole, W*Xscole, Yscole*H);
            
        }
       
        
        return temptect;
    }
    @end
    

      

    就是这样了,在使用的时候,具体就是这样的

    //在创建基于 UIView 的试图控件的时候,Frame先不设定,如在创建一个UIImageView的时候:frame 之间用UIView 调用,就可以适应其他屏幕了
    
     UIImageView * imgView = [[UIImageView alloc] initWithFrame:[UIView GetRectWithX:30 Y:30 Width:100 Heigth:100]];
            
    

      

  • 相关阅读:
    EF框架下的双表查询
    MVC中控制器向视图传值的四种方式
    html中常用的标签元素
    配置sql server 允许远程连接
    Sql Server中使用存储过程来实现一些时间差的改变
    Linux学习资料整理
    后缀数组求字符串最长重复子串
    static关键字
    ubuntu下搭建hadoop平台
    机器学习中的一些概念
  • 原文地址:https://www.cnblogs.com/xujiahui/p/6042519.html
Copyright © 2011-2022 走看看