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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值