iOS 7 教程:让程序同时支持iOS 6和iOS 7

来源:互联网 发布:数组实现学生管理系统 编辑:程序博客网 时间:2024/05/03 02:04

原文:http://beyondvincent.com/blog/2013/11/19/122-working-with-ios-6-and-7/

注:本文由破船译自Itty Bitty Labs。

  1. iOS 7中的布局问题
  2. iOS 6运行异常
  3. Xcode 4编译错误
  4. UILabel不一致的background
  5. 全屏时隐藏状态栏
  6. UIToolbar barStyle
  7. 更多

由于各种原因,我们的程序需要同时支持iOS 7以及之前的版本(例如iOS 6),也就是说开发者不得不同时在iOS 7和iOS 6之间进行开发。实际上开发者对此是比较讨厌的。

iOS 7中的布局问题

下面是非常简单的一个程序,运行在iOS 6中的界面:

而要是运行在iOS 7的模拟器中,会看不到label了:

这是为什么呢?我们对其reveal一下看看吧:

从上图可以看出,实际上label躲在NavigationBar后面了。在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。

修复这个问题的快速方法就是在方法- (void)viewDidLoad中添加如下一行代码:

1
self.edgesForExtendedLayout = UIRectEdgeNone;

这样问题就修复了。

iOS 6运行异常

现在如果在iOS 6中运行程序,会遇到下面这样的运行时异常错误:

1
[LAViewController setEdgesForExtendedLayout:]: unrecognized selector sent to instance 0x778a210

所有只能在iOS 7中运行的API需要重新封装一下,如下代码所示:

1234
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]){    self.edgesForExtendedLayout = UIRectEdgeNone;}

Xcode 4编译错误

有些机器可能还在使用Xcode 4.6,当用4.6来编译代码时,会遇到下面的编译错误:

12
Property 'edgesForExtendedLayout' not found on object of type 'LAViewController *'Use of undeclared identifier 'UIRectEdgeNone'

为了避免这个错误,可以创建下面的这个宏:

123
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000#define IOS7_SDK_AVAILABLE 1#endif

然后在需要的地方将iOS 7的代码包装一下即可:

123
#ifdef IOS7_SDK_AVAILABLE...#endif

UILabel不一致的background

对于UILabel,在iOS 7中它的background颜色默认是clearColor,而在iOS 6中默认的是白色。所以,我们最好在代码中对label的background颜色进行明确的设置:

1
view.backgroundColor = [UIColor clearColor];

全屏时隐藏状态栏

在iOS 6中,当调用presentViewController时,默认的modal screen将是全屏(UIModalPresentationFullScreen)。为了在iOS 7中也能获得相同的效果,我们可以在modal controller中添加如下代码:

1234
- (BOOL)prefersStatusBarHidden{  return YES;}

UIToolbar barStyle

有时候,我们会将UIToolbar与系统键盘结合起来使用。而在iOS 6中的键盘是黝黑色的,此时toolbar的style一般也是类似的,如下代码所示:

1
self.barStyle = UIBarStyleBlack;// or UIBarStyleBlackTranslucents

而在iOS 7中,键盘变为了亮色,因此我们需要根据不同的iOS 版本,设置不同的bar style。

12345678
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending){    self.barStyle = UIBarStyleDefault;}else{    self.barStyle = UIBarStyleBlack;//or UIBarStyleBlackTranslucent}

更多

上面这些技巧是我目前在开发中遇到的,肯定还有更多的技巧,大家要是知道的话可以告诉我。

最后送大家一个图,看看相关差异吧:

补充:

在ios7里面如果有滚动视图的时候,比如UITableView,UIScrollView的时候有时候会偏移20像素。这时候可以这样设置
if (IOS7) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
其中IOS7是一个判断当前系统版本是否为iOS7的宏


还有就是UIAlertView不再支持添加subview。。。比如text field输入框。但它自带的有
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
};

如果不是自定义的alertView只需要设置下style就可以。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"" otherButtonTitles:@"", nil];
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField *textField = [alertView textFieldAtIndex:0]; 
textField.placeholder = @"请输入用户名"; //设置对应textfield
[alertView show];

0 0