iOS6 应用向 iOS7 的快速迁移

随着 iOS7 发布的临近,原来的几个 iOS6 小应用在 iOS7 下试运行,发现闪退现象发生的很少,比 iOS4 升 iOS5 时在内存管理方面好多了。

但 iOS7 下界面的展示与 iOS6 差异较多,做为老程序员还是很怀念iOS6的风格,眼看着 iOS7 发布日期越来越近了,现在找些办法,让原来的应用在 iOS7 下先可以正常操作,后面再来改进界面吧。

除了图标变化等等之外,对于基于 ViewContoller 的应用迁移,遇到了下面的问题,总结了一些可能不是最好的解决办法:

1. iOS7 状态栏与 ViewController 进行融合,至使状态栏与 ViewController 中的 View 内容发生叠加,网上有些将 Window 下移 20 px,但发现在应对屏幕旋转的时候,表现并不好,用一个笨办法将所有上边距固定的view 下移 20 个px:

void adjustSubViewForIOS7StatusBar(UIView *baseView)
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        for (UIView *view in baseView.subviews) {
            if ((view.autoresizingMask & UIViewAutoresizingFlexibleTopMargin) != UIViewAutoresizingFlexibleTopMargin) {
                if (view.autoresizingMask & UIViewAutoresizingFlexibleHeight) {
                    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + 20, view.frame.size.width, view.frame.size.height -20);
                }else{
                    view.frame = CGRectOffset(view.frame, 0, 20);
                }
            }
        }
    }
}

2.  新的 view 中增辑 tintColor 的属性,由于 iOS6 以前以灰色调为主,可以在 viewController 中设置缺省的 tintColor 为 GrayColor,在iOS7 下与 iOS 6 的色系基本一致。如果一个一个控件设置,一是在 iOS6的表现与原来差异过大,另外像 toolbar, segmentControl 中 tintColor 被挪为他用,想在 iOS6 和 iOS7 取得平衡很困难;

3.  UIButton 在 iOS7 中完全被扁平化,如果还是 iOS6 的 UI,会差异很大,为此定位从 UIButton 继承创建子类 UIButton6,在 initWithCoder 和 initWithFrame 以及 buttonWithType 方法中,加入iOS6 类似的圆角按钮图片做为背景;

@implementation UIButton6

UIImage *gButtonImage;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        if (gButtonImage == nil) {
            gButtonImage = [[UIImage imageWithNamed:@"roundRect.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
        }
        [self setBackgroundImage:gButtonImage forState:UIControlStateNormal];
	}
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (gButtonImage == nil) {
            gButtonImage = [[TgwooImage imageFromString:button6] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
        }
        [self setBackgroundImage:gButtonImage forState:UIControlStateNormal];
	}
    return self;
}

+ (id)buttonWithType:(UIButtonType)buttonType
{
    UIButton6 *button = [UIButton buttonWithType:buttonType];
    if (button) {
        if (gButtonImage == nil) {
            gButtonImage = [[TgwooImage imageFromString:button6] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
        }
        [button setBackgroundImage:gButtonImage forState:UIControlStateNormal];
	}
    return button;
}


@end

4. 在 iOS7 中不仅 uniqueIdentifier 不能使用了, 设备的 mac 地址也获取不到了。 不过好在苹果为每个开发商还是提供了开发商范围内的设备唯一标识,

[[UIDevice currentDevice] identifierForVendor],也解决了设备唯一性判断的问题。


5.TableViewCell 如果是用 xib 方式来定义的,xib 中的输入框和按钮的 User Interaction Enabled 好像被禁用了,试了好长的时间,发现应该是被放到了 ContentView 的下面,响应不了用户点击的事件。解决的方法是在 TableViewCell 中加一个顶层的 View 并关联到一个自定义的 viewContent Outlet 上,然后用

[tableViewCell.contentView addSubview:viewContent];

来解决。



猜你喜欢

转载自blog.csdn.net/whyliu_/article/details/11394173