天天看点

php masonry,记录一次更新Masonry的问题

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

-[UIView mas_topMargin]: unrecognized selector sent to instance

原因

导致闪退的原因,其实是使用了__IPHONE_OS_VERSION_MIN_REQUIRED这个宏去判断最低支持的 iOS 版本。但是从 Cocoapods 拉取下来的 Masonry,最低版本是 6.0 。于是这个宏定义的判断是不生效的,因此导致了 unrecognized selector sent to instance 这类问题。

#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 开始才支持动态库的缘故。

并不是很推荐的做法

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

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

比较推荐的做法

因为是使用 Cocoapods,所以其实可以考虑从 Podfile 上入手。只需要在 Podfile 中加入一些配置就 OK 了。

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

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,会把所有第三方库都更改了。

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