而這個問題也讓我苦惱了一個禮拜,網路上沒有非常好的解法(stackoverflow有,不過你要看
的懂他寫的,而且解法非完美要結合另一篇才行)
首先,概念其實很簡單,為每一個xib檔案配一個不同尺寸的xib檔(也就是各尺寸各一)
然後,判定現在的機型是哪一種,iphone 4s ? 5/5s? 6? 6 plus? 決定要套用哪一個xib檔案
該怎樣判定呢?
先定義螢幕長寬吧!
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define iPhone4s_or_less (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define iphone5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define iphone6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define iphone6plus (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
接著我們需要在initWithNibName()這邊判斷
現在是4s,5,6,6plus
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (iphone6plus) {
NSLog(@"iphone 6 +");
self = [super initWithNibName:@"Page2_6_plus" bundle:nibBundleOrNil];
}
else if(iphone6)
{
NSLog(@"iphone 6");
self = [super initWithNibName:@"Page2_6" bundle:nibBundleOrNil];
}
else if (iphone5) {
NSLog(@"iphone 5/5s ");
self = [super initWithNibName:@"Page2_5s" bundle:nibBundleOrNil];
}
else{
NSLog(@"iphone 4s ");
self = [super initWithNibName:@"Page2" bundle:nibBundleOrNil];
}
return self;
}
當然,也是要把xib檔案複製->改名為相對應的名稱,這樣一來,就完成囉!
end