Iphone App 按比例手动布局(待完善)

来源:互联网 发布:中国移动有5g网络吗 编辑:程序博客网 时间:2024/06/16 14:57

借鉴:http://blog.csdn.net/q862171879/article/details/44079369

第一次发表博客,只是做一个尝试,对于问题的考虑真的不是特别全面,希望读到这篇博客的人能够给我指出问题一起进步,感谢曾经分享的人

由于我们的UI是以iphone5s为基准做的设计 ,我拿到设计图的时候,所有的 UI视图都是以 640 * 1136 来进行基础设计的 ,而且,我们的App只考虑 iphone端,而没有 ipad,那么就让我这个不习惯不喜欢而且也不太会autolayout的人有了可乘之机,我使用了按比率(iphone尺寸)布局.

首先我考虑的是iphone 设备的几个版本 iphone 4,4s ,5,5s ,6,6P

(图是网上找的,算不算侵权)

我们发现,4,5,6,6+的唯有高度不一样,那么我们就可以找到突破点了,根据设备高度写一个方法吧


    if (IPhone4) {

        

    }elseif (Iphone5){

        

    }elseif (Iphone6){

        

    }elseif (IPhone6P){

        

    }

你突然发现,这样写是不是很失水准如果出了7了呢,可是这种方式 看着明白啊,尤其是像我这样的新手,看着明白啊,既然我们说的是按比例,那么我们就要按比例哦

先定义两个比例

@propertyfloat autoX;//x缩放比

@propertyfloat autoY;//y缩放比例

定义好了,这俩比例怎么用呢? 当然是根据不同设备来赋值不同比例了,想想怎么做~~~~~~哦,根据屏幕高度来判断


  if(kScreenHeight > 568){

        self.autoX = kScreenWidth/320;

        self.autoY = kScreenHeight/568;

   }else{

         self.autoX = 1.0;

         self.autoY = 1.0;

   }

好了,完事了,你会发现 4,5控件等比例,6,6p是放大的

那4,5怎么适配呢5的高度是568 ,4的高度是480,完了,假如一个控件在5上的frame 为(0,480,100,100),4上不就看不到了么

好吧,我承认,我也很纠结这个问题,可是我发现了,我们大部分控制器视图都是TableView ,所以不必计较哦,假如~~~~~~有一个控制器视图不是TableView,那么请这位码农同志在控制器视图上添加个ScrollView,再继续做


好吧,上代码了,不扯了,偷偷用了点上班时间,老板不会发现了吧!!!


.h文件

@interface LayOutManger :NSObject


//我是写成单例类哦,大家可以随自己高兴

+ (LayOutManger *)sharedInstance;

@propertyfloat autoX;//x缩放比例

@propertyfloat autoY;//y缩放比例


- (CGRect)WithX:(float)x

          WithY:(float)y

      WithWidth:(float)width

     WithHeight:(float)height;//替代CGRectMake(x,y,w,h)


- (float)WithX:(float)x;//替代x坐标

- (float)WithY:(float)y;//替代y坐标

- (float)WithWidth:(float)width;//替代宽度

- (float)WithHeight:(float)height;//替代高度


.m文件

- (CGRect)WithX:(float)x

          WithY:(float)y

      WithWidth:(float)width

     WithHeight:(float)height

{

    [self_layout];

    

   CGRect rect;

    rect.origin.x = x *self.autoX;

    rect.origin.y = y *self.autoY;

    rect.size.width = width *self.autoX;

    rect.size.height = height *self.autoY;

   return rect;

}


- (float)WithX:(float)x{

    [self_layout];

    return x *self.autoX;

}


- (float)WithY:(float)y{

    [self_layout];

    return y *self.autoY;

}


- (float)WithWidth:(float)width{

    [self_layout];

   return width * self.autoX;

}


- (float)WithHeight:(float)height{

    [self_layout];

   return height * self.autoY;

}


- (void)_layout{

    if(kScreenHeight >568){

       self.autoX =kScreenWidth/320;

       self.autoY =kScreenHeight/568;

    }else{

        self.autoX =1.0;

        self.autoY =1.0;

    }

}



好吧,代码就这些了,不管写的如何,希望大家能够学习到东西,再次感谢原作者(思想我偷了一点,不会打我吧)


//.pch中哦

#define KCGRect(x,y,w,h)            [[LayOutManger sharedInstance]WithX:x WithY:y WithWidth:w WithHeight:h]

#define KCGRectWithx(x)             [[LayOutManger sharedInstance]WithX:x]

#define KCGRectWithy(y)             [[LayOutManger sharedInstance]WithY:y]

#define KCGRectWithwidth(width)     [[LayOutManger sharedInstance]WithWidth:width]

#define KCGRectWithheight(height)   [[LayOutManger sharedInstance]WithHeight:height]



那么 KCGRect(x,y,w,h) 替代了CGRectMake....









0 0
原创粉丝点击