iOS螢幕適配(尺寸適配)

來源:互聯網
上載者:User

標籤:lob   補充   and   iphone   ima   details   detail   sys   get   

 

螢幕適配在用Masonry、或者其他的感覺有點用不習慣,螢幕尺寸適配是我在前輩哪兒雪來的,簡單分享一下我的寫法,不成熟之處望多多指教,僅供參考:建議跟著代碼敲一遍,以後自己用者熟悉方便,原理也很簡單;廢話有點多了,上

 

建立項目-建立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]];        

  

 

iOS螢幕適配(尺寸適配)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.