iOS 使用 Interface Builder 兼容 iOS6 和iOS7
2014-09-07 22:33
561 查看
当你在更新你的App到iOS7的平台时遇到最大的挑战之一就是确保不要遗忘那些还在使用iOS6平台的用户,在此我们提供一些建议使你的App应用在iOS6和iOS7上同时保留视觉吸引力和技术功能.
此图为InterfaceBuilder中顶部和底部布局指南
设置正确的InterfaceBuilderStoryboard或者正确设置XIBs文件,对于iOS6和iOS7的开发大有帮助,使得开发更加容易。我们的第一个建议是为全部的UIViewController创建Storyboard,UIViewControl是合成的或者是跨越整个屏幕高度的控制试图(ControlViews).如果你正在做的项目是使用XIBs文件开发的,而没有使用Storyboards,那么Storyboards可以尽可能的简化为带有视图容器(ViewContainer)的“UIViewController",将来你可以从NIB文件加载。我们这样建议的主要原因是这样操作你可以获得访问在Interface
Builder中顶部和底部布局指南的权限。
InterfaceBuilder中”Viewas“配置,这个设置是每个文件(NIB或者Storyboard)的基础设置。
另外,我们还建议您把所有的XIBs和Storyboard中的Viewas设置为iOS7andLater,这种设置方式将更加容易降低未来的iOS6的支持,此外,如果您没有使用自动布局,那么就会更加容易更加直观的使用iOS
6/7Delta。
AdjustingfortheStatusBar
IniOS7,theareabehindthestatusbarbecomes
useablescreenspace.Asaresult,thewindowiniOS7is20pointstallerthanitisiniOS6.ThiscancausecontentatthetopofthescreentoappearunderneaththestatusbariniOS7.AdjustingviewstomakeuseoforavoidthisextraspaceiniOS7
frequentlycausesviewstobemisplacediniOS6.
Ifyourprojectdoesn’tuseAutoLayout,issuescaused
bythestatusbardifferencesbetweeniOS6andiOS7canberesolvedbyusingSprings,Struts,andiOS6/7Deltas.Inordertopreventthecontentfrombeingplacedunderthestatusbar,simplymovethecontentdown,howevermanypointsarenecessary,to
getthecontentoutfromunderthestatusbar.However,thiscausesthecontenttobemoveddowniniOS6aswell,whichwedidn’twant,sincethecontentwasn’tunderthestatusbariniOS6.Toremedythis,useiOS6/7DeltastosetanegativeYDeltaequal
tothenumberofpointsthatwemovedthecontentdownforiOS7.Forexample,ifwemovedcontentdown20pointsforiOS7,thenwesetaYDeltaof-20points.Forcontentnearthebottomofthescreen,itisofteneasierandmorerobusttosimplyusecorrectly
configuredstrutsandsprings,insteadofiOS6/7Deltas,topositionthecontentrelativetothebottomofthescreen.Averysimilartacticcanbeusedwhenadjustingviewsthatspantheentireheightofthescreen.Adjusttheheightand/ororigininyour
StoryboardorXIBtoachievethesizeandpositionyouwouldlikeiniOS7,andthenuseYand/orHeightDeltas,incombinationwithSpringsandStruts,toresolvethesizeand/orpositioniniOS6.Below,Ihaveprovidedasimpleexample,usingtwoimage
views(oneatthetopofthescreenandoneatthebottomofthescreen).
AutoLayoutmakessolvingissuescausedbythestatusbardifferencessimpler,assumingyouareusingStoryboards,asmentionedbefore.SimplyusetheTopandBottomLayoutGuidesprovidedbyInterfaceBuilder.Ifyoumakeyourtopandbottom
relativetotheTopandBottomLayoutGuides(insteadofthetopandbottomoftherootview),theniOSwillautomaticallymaketheadjustmentsnecessarytohaveeverythinglaidoutonscreenandoutfromunderthestatusbar,inbothiOS6andiOS7.Ifyou
aren’tabletouseStoryboards,youwilllikelyneedtousecodetoaccesstheTopandBottomLayoutGuides.Belowisthesameexampleasbefore,butresolvedusingAutoLayoutand
thatarerelativetotheTopandBottomLayoutGuides.
Andnow,regardlessofwhetherStruts,Springs,andiOS6/7DeltasorAutoLayoutwithTopandBottomLayoutGuidesareused,everythingislaidoutcorrectlyiniOS6andiOS7.
AdjustingforaUINavigationControllerwithiOS7′sTranslucentUINavigationBar
Translucent
settingforUINavigationBarinInterfaceBuilder.
OneofthemorecommonchallengeswhensimultaneouslysupportingiOS6andiOS7withInterfaceBuilderissettinga
translucentforiOS7.Makingthe
Extend
EdgessettinginInterfaceBuilder.
If
thattogglingthe
Builderwillcausetheviewstomoveupanddown.Settingthe
iOS7.IniOS6nothingchanges;theviewsstaybelowthe
deprecationiniOS6.Youwillnoticethatthe
nowmarkedasdeprecated,andtheother
iniOS6.Thiscausesasituationverysimilartothenewstatusbarthatwediscussedintheprevioussection,wheretheuseablescreenspaceistalleriniOS7thanitisiniOS6.
Mostoftheissuesthatadjustingthetranslucencyofthe
anextrastepmaybenecessaryifoneormoreoftheviewsthatgetsmovedupunderthe
asubclassof
tobuildoutthe
thenthecontentinsetswilllikelygetsetautomatically.Theautomaticallyadjustedcontentinsetsensurethatthecontentwithinthe
offsetthatputsitbelowthe
subclass)thatgetsmovedupunderthe
thenyoumayneedtosetthecontentinsetsyourself.
ItispossibletosetcontentinsetsinInterfaceBuilder,but,inthiscase,Iwouldsuggestdoingsoincode,foracoupleofreasons.First,weonlywanttosettheinsetsforiOS7andnotiOS6,whichisnotpossibleinInterfaceBuilder.Second,since
weonlywanttosetthecontentinsetswhenrunninginiOS7,wecanusetheTopLayoutGuidetohelpussetthecontentinsets(evenifyourStoryboardorXIBdoesn’tuseAutoLayout).Belowisanexampleofhowthiscanbeaccomplished.Intheexample,I
useda
BesureyouareoniOS7orlaterbeforeaccessingthe
MakingyourappvisuallyappealingandfullyfunctionalonbothiOS6and7canbequitethechallenge,particularlysinceeveryappisuniquelyconstructed.AnticipatingandcoveringeverypossiblesituationthatdeveloperswillrunintowhensupportingiOS
6andiOS7simultaneouslyisnearlyimpossible;hopefully,thisarticlewillhelpwithsomecommoncasesandputyouontherighttrackforhandlingtherareorcomplexcases.
此图为InterfaceBuilder中顶部和底部布局指南
设置正确的InterfaceBuilderStoryboard或者正确设置XIBs文件,对于iOS6和iOS7的开发大有帮助,使得开发更加容易。我们的第一个建议是为全部的UIViewController创建Storyboard,UIViewControl是合成的或者是跨越整个屏幕高度的控制试图(ControlViews).如果你正在做的项目是使用XIBs文件开发的,而没有使用Storyboards,那么Storyboards可以尽可能的简化为带有视图容器(ViewContainer)的“UIViewController",将来你可以从NIB文件加载。我们这样建议的主要原因是这样操作你可以获得访问在Interface
Builder中顶部和底部布局指南的权限。
InterfaceBuilder中”Viewas“配置,这个设置是每个文件(NIB或者Storyboard)的基础设置。
另外,我们还建议您把所有的XIBs和Storyboard中的Viewas设置为iOS7andLater,这种设置方式将更加容易降低未来的iOS6的支持,此外,如果您没有使用自动布局,那么就会更加容易更加直观的使用iOS
6/7Delta。
AdjustingfortheStatusBar
IniOS7,theareabehind
useablescreenspace.Asaresult,thewindowiniOS7is20pointstallerthanitisiniOS6.ThiscancausecontentatthetopofthescreentoappearunderneaththestatusbariniOS7.AdjustingviewstomakeuseoforavoidthisextraspaceiniOS7
frequentlycausesviewstobemisplacediniOS6.
ContentundertheStatusBariniOS7. | ContentbeingpushedoffofthebottomofthescreeniniOS6. |
bythestatusbardifferencesbetweeniOS6andiOS7canberesolvedbyusingSprings,Struts,andiOS6/7Deltas.Inordertopreventthecontentfrombeingplacedunderthestatusbar,simplymovethecontentdown,howevermanypointsarenecessary,to
getthecontentoutfromunderthestatusbar.However,thiscausesthecontenttobemoveddowniniOS6aswell,whichwedidn’twant,sincethecontentwasn’tunderthestatusbariniOS6.Toremedythis,useiOS6/7DeltastosetanegativeYDeltaequal
tothenumberofpointsthatwemovedthecontentdownforiOS7.Forexample,ifwemovedcontentdown20pointsforiOS7,thenwesetaYDeltaof-20points.Forcontentnearthebottomofthescreen,itisofteneasierandmorerobusttosimplyusecorrectly
configuredstrutsandsprings,insteadofiOS6/7Deltas,topositionthecontentrelativetothebottomofthescreen.Averysimilartacticcanbeusedwhenadjustingviewsthatspantheentireheightofthescreen.Adjusttheheightand/ororigininyour
StoryboardorXIBtoachievethesizeandpositionyouwouldlikeiniOS7,andthenuseYand/orHeightDeltas,incombinationwithSpringsandStruts,toresolvethesizeand/orpositioniniOS6.Below,Ihaveprovidedasimpleexample,usingtwoimage
views(oneatthetopofthescreenandoneatthebottomofthescreen).
InterfaceBuildersettingsforthetopUIImageViewtokeepitdirectlyundertheStatusBariniOS7andiOS6. | InterfaceBuildersettingsforthebottomUIImageViewtopreventitfrombeingpushedoffofthebottomofthe screen. |
NSLayoutConstraints
relativetotheTopandBottomLayoutGuides(insteadofthetopandbottomoftherootview),theniOSwillautomaticallymaketheadjustmentsnecessarytohaveeverythinglaidoutonscreenandoutfromunderthestatusbar,inbothiOS6andiOS7.Ifyou
aren’tabletouseStoryboards,youwilllikelyneedtousecodetoaccesstheTopandBottomLayoutGuides.Belowisthesameexampleasbefore,butresolvedusingAutoLayoutand
NSLayoutConstraints
thatarerelativetotheTopandBottomLayoutGuides.
ThetopUIImageView’stopconstraintsetrelativetoTopLayoutGuide. | ThebottomUIImageView’sbottomconstraintsetrelativetoBottomLayoutGuide. |
iOS7aftermakingtheadjustmentsinInterfaceBuilder. | iOS6aftermakingtheadjustmentsinInterfaceBuilder. |
Translucent
settingforUINavigationBarinInterfaceBuilder.
OneofthemorecommonchallengeswhensimultaneouslysupportingiOS6andiOS7withInterfaceBuilderissettinga
UINavigationController’s
UINavigationBarto
translucentforiOS7.Makingthe
UINavigationBartranslucentcanhaveafairlylargeimpactonyourviewsandwilllikelyrequireyoutomakesomechanges.
Extend
EdgessettinginInterfaceBuilder.
If
ExtendEdgesissetto
UnderTopBarsforaUIViewController,youwillnotice
thattogglingthe
Translucentsettingonthe
UINavigationBarinInterface
Builderwillcausetheviewstomoveupanddown.Settingthe
UINavigationBartotranslucentcausesviewstomoveupunderthe
UINavigationBarin
iOS7.IniOS6nothingchanges;theviewsstaybelowthe
UINavigationBar.Thisisduetotranslucent
UINavigationBars
deprecationiniOS6.Youwillnoticethatthe
BlackTranslucent
UIBarStyleis
nowmarkedasdeprecated,andtheother
UIBarStylescreateopaque
UINavigationBars
iniOS6.Thiscausesasituationverysimilartothenewstatusbarthatwediscussedintheprevioussection,wheretheuseablescreenspaceistalleriniOS7thanitisiniOS6.
Mostoftheissuesthatadjustingthetranslucencyofthe
UINavigationBarcreatescanbefixedusingthesamemethodologyasdiscussedintheprevioussection.However,
anextrastepmaybenecessaryifoneormoreoftheviewsthatgetsmovedupunderthe
UINavigationBarisa
UIScrollView(or
asubclassof
UIScrollView,suchas
UITableView).IfyouareusingaStoryboard
tobuildoutthe
UIScrollView(orsubclass)thatgetsmovedupunderthe
UINavigationBar,
thenthecontentinsetswilllikelygetsetautomatically.Theautomaticallyadjustedcontentinsetsensurethatthecontentwithinthe
UIScrollViewwillhaveaninitial
offsetthatputsitbelowthe
UINavigationBar,and,thus,immediatelyvisibletotheuser.However,ifthe
UIScrollView(or
subclass)thatgetsmovedupunderthe
UINavigationBarishandledinalessdirectway(forexampleifitisbuiltinaXIBandlaterloadedintoacontainerview),
thenyoumayneedtosetthecontentinsetsyourself.
ItispossibletosetcontentinsetsinInterfaceBuilder,but,inthiscase,Iwouldsuggestdoingsoincode,foracoupleofreasons.First,weonlywanttosettheinsetsforiOS7andnotiOS6,whichisnotpossibleinInterfaceBuilder.Second,since
weonlywanttosetthecontentinsetswhenrunninginiOS7,wecanusetheTopLayoutGuidetohelpussetthecontentinsets(evenifyourStoryboardorXIBdoesn’tuseAutoLayout).Belowisanexampleofhowthiscanbeaccomplished.Intheexample,I
useda
UITableViewinsteadofa
UIScrollViewsincethatisamorecommoncase.
BesureyouareoniOS7orlaterbeforeaccessingthe
topLayoutGuidepropertyof
UIViewController.
1 | if ([[[UIDevicecurrentDevice]systemVersion]floatValue]>=7.0f){ |
2 | CGFloattopInset=self.tableView.contentInset.top+[self.topLayoutGuidelength]; |
3 | CGFloatleftInset=self.tableView.contentInset.left; |
4 | CGFloatbottomInset=self.tableView.contentInset.bottom; |
5 | CGFloatrightInset=self.tableView.contentInset.right; |
6 |
7 | self.tableView.contentInset=UIEdgeInsetsMake(topInset,leftInset,bottomInset,rightInset); |
8 | } |
6andiOS7simultaneouslyisnearlyimpossible;hopefully,thisarticlewillhelpwithsomecommoncasesandputyouontherighttrackforhandlingtherareorcomplexcases.
相关文章推荐
- ios下使用VFL兼容ios6和ios7下的头部显示问题
- 使用 Interface Builder 兼容 iOS6 和iOS7
- 使用 Interface Builder 兼容 iOS6 和iOS7
- [iOS] iOS6 iOS7 访问和使用系统通讯录
- ios--ios6与ios7界面兼容(解决方案)
- 使用 Interface Builder 兼容 iOS6 和iOS7
- iOS:如何让xib同时兼容支持iOS6和iOS7
- iOS兼容开发:让程序同时支持iOS6和iOS7
- 使用 Interface Builder 兼容 iOS6 和iOS7
- [iOS diary]iOS6样式兼容之 NavigationBar和TabBar 样式扁平化 兼容iOS7样式 全局实现
- 使用 Interface Builder 兼容 iOS6 和iOS7
- ios--ios6与ios7界面兼容(解决方案)
- iOS开发 -- IOS7界面兼容,从4.3到7.0兼容的NavigationBar ,IOS7 NavigationBar 适配
- iOS- <项目笔记>iOS6 & iOS7屏幕图片适配
- ios6 ios7 同时兼容
- iOS开发 _ iOS7的兼容之上移20
- iOS6兼容iOS7界面(界面上移,navigationbar 颜色)
- 兼容IOS7和IOS6
- 【iOS开发】iOS7 兼容及部分细节
- IOS中Cell高度的自适应(ios6、ios7)