今天遇到一個問題,項目中使用到了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