记录一次更新Masonry的问题

今天遇到一个问题,项目中使用到了Masonry这个知名的第三方库。由于 Pod-Masonry-iOS Deployment Target 是 6.0,致使项目中使用到mas_topMargin等属性会报错,而后致使了闪退。git

-[UIView mas_topMargin]: unrecognized selector sent to instance

缘由

致使闪退的缘由,实际上是使用了__IPHONE_OS_VERSION_MIN_REQUIRED这个宏去判断最低支持的 iOS 版本。可是从 Cocoapods 拉取下来的 Masonry,最低版本是 6.0 。因而这个宏定义的判断是不生效的,所以致使了 unrecognized selector sent to instance 这类问题。github

#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000)

- (MASConstraint *)leftMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin];
}

- (MASConstraint *)rightMargin {
    return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin];
}
....

解决方法

值得一提的是,若是 Podfile 使用了use_frameworks!,那么上述问题是能够解决的,Pod-Masonry-iOS Deployment Target 也会改为 iOS 8.0。猜想有多是 iOS 8 开始才支持动态库的缘故。objective-c

并非很推荐的作法

手动把 Pod-Masonry-iOS Deployment Target 改为 iOS 8.0 。可是这样作的话,有个弊端,就是再次 pod install 或者 pod update,那么仍是会变成 iOS 6.0 。app

就像我以前就遇到这问题了。可是没有考虑从根源上去解决。因而最近 pod install 后,发现这里就变会 6.0 了。最后致使 app 在 iOS 9 上闪退。post

比较推荐的作法

由于是使用 Cocoapods,因此其实能够考虑从 Podfile 上入手。只须要在 Podfile 中加入一些配置就 OK 了。ui

比较柔和的 Podfile,只会更改 Masonry 。code

target 'ObjcDemo' do

    pod 'YYText', '~> 1.0.7'
    pod 'Masonry'

end

# 加入这些配置
post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Masonry"
            target.build_configurations.each do |config|
                config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
            end
        end
    end
end

另一种比较简单粗暴的 Podfile,会把全部第三方库都更改了。get

target 'ObjcDemo' do

    pod 'YYText', '~> 1.0.7'
    pod 'Masonry'

end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
        end
    end
end
相关文章
相关标签/搜索