IOS开发之屏幕适

来源:互联网 发布:日本tpp知乎 编辑:程序博客网 时间:2024/06/04 23:19

开发适配是必不可少的一个步骤,下面总结一下适配的方法

1. 根据屏幕宽/高不同,来进行适配

(1) 宏定义

// collectionView x的值

#define CollectionViewX 0

// collectionView y的值

#define CollectionViewY 120

// segment y 的值

#define SegmentY 75

//             的宽

#define SegmentWidth 200

//             的高

#define SegmentHeight 40



- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColorwhiteColor];

    

   

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayoutalloc]init];

    if (self.view.frame.size.width == 375) {

        flowLayout.itemSize =CGSizeMake(150,100);

        

    }

    if (self.view.frame.size.width == 320) {

        flowLayout.itemSize =CGSizeMake(120,80);

    }

    if (self.view.frame.size.width == 414) {

        flowLayout.itemSize =CGSizeMake(160,110);

    }

    

    

    flowLayout.minimumInteritemSpacing =10;

    flowLayout.minimumLineSpacing =10;

    

    flowLayout.sectionInset =UIEdgeInsetsMake(10,30,50,30);

    self.collectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(CollectionViewX,CollectionViewY,self.view.frame.size.width,self.view.frame.size.height - CollectionViewY)collectionViewLayout:flowLayout];

    self.collectionView.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:self.collectionView];

    self.collectionView.delegate =self;

    self.collectionView.dataSource =self;

    }


2.宏定义屏幕的尺寸, 用宏定义FRAME尺寸与原来坐标尺寸相除得到比例,进行适配, 有点恶心这个

(1)宏定义

#define X 5

#define Y 5

#define W 100

#define H 150

#define FRAME  [[UIScreen mainScreen] bounds]


- (void)createSubView

{

    CGRect frame =CGRectMake(FRAME.size.width/75,FRAME.size.height/133.6,FRAME.size.width/3.75,FRAME.size.height/4.453);

   self.imageView1 = [[UIImageViewalloc]initWithFrame:frame];

    self.imageView1.backgroundColor = [UIColorwhiteColor];

    [self.contentViewaddSubview:self.imageView1];

    

    frame =self.imageView1.frame;

    frame.origin.x += (frame.size.width + FRAME.size.width/37.5);

    frame.size.width =FRAME.size.width/9.375;

    frame.size.height =FRAME.size.height/16.7;

   self.categorylabel1 = [[UILabelalloc]initWithFrame:frame];

    self.categorylabel1.backgroundColor = [UIColorwhiteColor];

    self.categorylabel1.text =@"类型";

    [self.contentViewaddSubview:self.categorylabel1];

    

    frame =self.categorylabel1.frame;

    frame.origin.x += (FRAME.size.width/37.5 + frame.size.width);

    frame.size.width =FRAME.size.width/1.875;

   self.categoryLabel2 = [[UILabelalloc]initWithFrame:frame];

    self.categoryLabel2.backgroundColor = [UIColorwhiteColor];

    [self.contentViewaddSubview:self.categoryLabel2];

    

    frame =self.categorylabel1.frame;

    frame.origin.y += (frame.size.height + FRAME.size.height/66.8);

   self.authorLabel1 = [[UILabelalloc]initWithFrame:frame];

    self.authorLabel1.backgroundColor = [UIColorwhiteColor];

    self.authorLabel1.text =@"作者";

    [self.contentViewaddSubview:self.authorLabel1];

    

    frame =self.categoryLabel2.frame;

    frame.origin.y += (FRAME.size.height/66.8 + frame.size.height);

   self.authorLabel2 = [[UILabelalloc]initWithFrame:frame];

    self.authorLabel2.backgroundColor = [UIColorwhiteColor];

    [self.contentViewaddSubview:self.authorLabel2];

    

    frame =self.authorLabel1.frame;

    frame.origin.y += (frame.size.height + FRAME.size.height/66.8);

   self.scoreLabel1 = [[UILabelalloc]initWithFrame:frame];

    self.scoreLabel1.backgroundColor = [UIColorwhiteColor];

    self.scoreLabel1.text =@"评分";

    [self.contentViewaddSubview:self.scoreLabel1];

    

    frame =self.authorLabel2.frame;

    frame.origin.y += (frame.size.height + FRAME.size.height/66.8);

   self.scoreLabel2 = [[UILabelalloc]initWithFrame:frame];

    self.scoreLabel2.backgroundColor = [UIColorwhiteColor];

    [self.contentViewaddSubview:self.scoreLabel2];

    


}


3.这个就比较简单了, 本例是在Iphone6的基础上做的适配,宽:375, 高:668, 即把宽高的比例算出来, 原坐标乘以这个比例就可以了,也就是X乘以宽的比例,Y乘以高的比例, 宽高分别乘以宽高的比例

(1)宏定义

#define W 150

#define H 40

#define W1     [[UIScreen mainScreen] bounds].size.width/375

#define H1      [[UIScreen mainScreen] bounds].size.height/668


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    

    self.button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    self.button1.backgroundColor = [UIColorclearColor];

   self.button1.frame =CGRectMake(25*W1,200*H1,W*W1, H*H1);

    

    

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button2.backgroundColor = [UIColorclearColor];

    button2.frame =CGRectMake(25*W1, (200 +H + 15)*H1,W*W1,H*H1);

    [button2 setBackgroundImage:[UIImageimageNamed:@"5.png"]forState:UIControlStateNormal];

    [

    

    UIButton *button3 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button3.backgroundColor = [UIColorclearColor];

    button3.frame =CGRectMake(25*W1, (200 +2 *(H +15))*H1, W*W1,H*H1);

   

    UIButton *button4 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button4.backgroundColor = [UIColorclearColor];

    button4.frame =CGRectMake(25*W1, (200 +3 *(H +15))*H1, W*W1,H*H1);

  

    

    

}




0 0
原创粉丝点击