iphone5适配 有如下集中种思路
1:利用自身的autoresize 加上代码实现
2:写两套xib
3:利用ios5+ 以上的autolayout
前两种方式应用比较广泛 ,因为毕竟国内还有部分应用ios4.3的用户
下面给第二种方式应用下的部分代码
- (id)init { NSString *clssName = NSStringFromClass([self class]); NSString *xibName = is4InchScreen() ?[NSString stringWithFormat:@"%@_4inch", clssName] : clssName; self = [super initWithNibName:xibName bundle:nil]; if (self) {} return self; }写个宏定义
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
在需要改变坐标的地方这么写:
mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 86, 320, 264+(iPhone5 ? 88:0))];
希望有帮助。。。
关键是区别设备的型号,然后使用不同的frame .我的做法是使用Category。 给UIDevice添加一个自动适配的方法
#define AUTO_RESIZE_TO_IPHONE5(r) UIAutoResizeToPhone5(r)
#define AUTO_POS_TO_IPHONE5(r) UIAutoPositionToPhone5(r)
typedef enum {
UIDeviceModelPhone,
UIDeviceModelPhone5,
UIDeviceModelPad,
} UIDeviceModelType;
@interface UIDevice (deviceModel)
- (UIDeviceModelType)deviceModel;
@end
CGRect
UIAutoResizeToPhone5(CGRect rect);
CGRect
UIAutoPositionToPhone5(CGRect rect);
在.m文件中 实现判断的方法
@implementation UIDevice (deviceModel)
- (UIDeviceModelType)deviceModel
{
if ([self userInterfaceIdiom] == UIUserInterfaceIdiomPad)
return UIDeviceModelPad;
if ([self userInterfaceIdiom] == UIUserInterfaceIdiomPhone &&
CGSizeEqualToSize(CGSizeMake(640, 1136),
[[UIScreen mainScreen] currentMode].size))
return UIDeviceModelPhone5;
return UIDeviceModelPhone;
}
@end
/* auto resize method */
CGRect
UIAutoResizeToPhone5(CGRect rect)
{
if ([[UIDevice currentDevice] deviceModel] == UIDeviceModelPhone5)
rect.size.height += 88;
return rect;
}
CGRect
UIAutoPositionToPhone5(CGRect rect)
{
if ([[UIDevice currentDevice] deviceModel] == UIDeviceModelPhone5)
rect.origin.y += 88;
return rect;
}
在使用的地方,引入这个文件
someView.frame = AUTO_RESIZE_TO_IPHONE5(CGRectMake(0, 0, 320, 480));
ios7 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { [application setStatusBarStyle:UIStatusBarStyleLightContent]; self.window.clipsToBounds =YES; self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20); self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height); }