适配iphone和iPad
2019-04-18 本文已影响0人
阳vs阴
一、分别开发iphone和iPad版本
二、兼容开发iphone和ipad
1、General -> Deployment info -> Devices ->Universal
image.png
xib:
2、选中要适配的xib,例view.xib
3、file -> duplicate,将文件命名为view_ipad.xib
4、右键view_ipad.xib,open as -> source code, 找到targetrRuntime,将其修改为 targetRuntime="iOS.CocoaTouch.iPad"
5、为UINib创建分类 UINib+Adapter
@interface UINib (Adapter)
+ (UINib *)nibAdapter:(NSString *)name bundle:(nullable NSBundle *)bundleOrNil;
@end
#import "UINib+Adapter.h"
@implementation UINib (Adapter)
+ (UINib *)nibAdapter:(NSString *)name bundle:(nullable NSBundle *)bundleOrNil{
UINib *nib;
if((UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)) {
NSString *ipad = [name stringByAppendingString:@"_ipad"];
nib = [UINib nibWithNibName:ipad bundle:bundleOrNil];
}else{
nib = [UINib nibWithNibName:name bundle:bundleOrNil];
}
return nib;
}
@end
调用方法nibWithNibName: bundle:
替换成nibAdapter: bundle:
UIStoryboard
前四步类似
5、同样建立分类
@interface UIStoryboard (Adapter)
+ (UIStoryboard*_Nullable)storyboardAdapter:(nonnull NSString*)name bundle:(nullable NSBundle*)storyboardBundleOrNil;
@end
#import "UIStoryboard+Adapter.h"
@implementation UIStoryboard (Adapter)
+ (UIStoryboard*)storyboardAdapter:(NSString*)name bundle:(nullable NSBundle*)storyboardBundleOrNil{
UIStoryboard *storyBoard;
if((UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)) {
NSString *ipad = [name stringByAppendingString:@"_ipad"];
storyBoard = [UIStoryboard storyboardWithName:ipad bundle:storyboardBundleOrNil];
}else{
storyBoard = [UIStoryboard storyboardWithName:name bundle:storyboardBundleOrNil];
}
return storyBoard;
}
@end
调用方法storyboardWithName:bundle:
替换成storyboardAdapter: bundle: