关于iOS7的版本适配问题(从iOS9向iOS7适配)

版本适配的原因在于🍎总是会更新它的API去代替老的一些API,但是又不能直接把老的摈弃掉,于是给我们这些开发人员带来了一个很严重的问题是做到版本适配,让app能在不同机型下都能运行。最近我了解了一些部分常见的适配,分享下下。

1:对于两个日期的判断,分别适用于iOS8以下和iOS8以上,

-(NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts;

-(NSComparisonResult)compareDate:(NSDate *)date1 toDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0);

2:对于UIAlertController弹出框的适配,因为其是在iOS8以后出现的,因此在iOS8以前系统的手机我们需要做些改变,可以有UIAlertView和UIActionSheet代替使用

self.datePicker.datePickerMode = UIDatePickerModeDate;
                    UIActionSheet* startsheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n"
                                                                            delegate:self
                                                                   cancelButtonTitle:nil
                                                              destructiveButtonTitle:nil
                                                                   otherButtonTitles:@"确定",
                                                 @"取消", nil];
                    startsheet.tag = 333;
                    //添加子控件的方式也是直接添加
                    [startsheet addSubview:self.datePicker];
                    [startsheet showInView:self.view];

效果图如下:

3:个人觉得最难解决的问题是横竖屏的适配问题iOS7以后横屏后view的frame会跟着改变,问题不是很大,但是iOS7以前的就不是这么回事,最近很苦恼,有一些经验之谈,比如横屏改变当前Controller视图View的frame可以通过设置frame为视图view.Bounds

还有就是两个监听屏幕改变的方法进行设置,如下

#pragma mark - rotation Notifies
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [_scrollView setFrame:self.view.bounds];
    
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [_scrollView setFrame:self.view.bounds];
    NSLog(@"监听:didRotateFromInterfaceOrientation %ld,now bounds:%@,%@,",(long)fromInterfaceOrientation,NSStringFromCGRect(self.view.bounds),NSStringFromCGRect(self.view.frame));
    
}

最后给大家一个小功能,快速切换横竖屏,代码献上

 1  if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
 2             SEL selector = NSSelectorFromString(@"setOrientation:");
 3             NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
 4             [invocation setSelector:selector];
 5             [invocation setTarget:[UIDevice currentDevice]];
 6             UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
 7             int val = interfaceOrientation == UIInterfaceOrientationPortrait ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait;
 8             [invocation setArgument:&val atIndex:2];
 9             [invocation invoke];
10         }
View Code

 

 

posted @ 2016-05-26 15:15  desunire  阅读(501)  评论(0编辑  收藏  举报