ios - Adaptive layout for iPad -
i following article building adaptive layout in ios building adaptive user interfaces ios 8.
it work fine on iphones . wan't give same effect on ipad. ipad adaptive layout not work.
here screenshot of application in iphone5 (green box represent view1 , yellow box represent view2)
1.portrait mode
- landscape mode
question: how can achieve same effect ipad?
expanded question: how set initial orientation of ipad in landscape mode?
the problem ipad both orientation represented regular.
one of solution add 2 iboutlet collection view controller want orientation change happened, example:
@iboutlet var landscapeconstraints: [nslayoutconstraint]! @iboutlet var portraitconstraints: [nslayoutconstraint]!
go storyboard, switch adaptive layout want happened in ipad portrait orientation , control drag every active constraints portraitconstraints iboutlet. change adaptive layout 1 want happened landscape , again control drag active constraints landscapeconstraints iboutlet.
in view controller override viewwilltransitiontosize
method:
override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { super.viewwilltransitiontosize(size, withtransitioncoordinator: coordinator) let transitiontolandscape = size.width > size.height let constraintstouninstall = transitiontolandscape ? ortraitconstraints : landscapeconstraints let constraintstoinstall = transitiontolandscape ? landscapeconstraints : portraitconstraints view.layoutifneeded() coordinator.animatealongsidetransition({ _ in nslayoutconstraint.deactivateconstraints(constraintstouninstall) nslayoutconstraint.activateconstraints(constraintstoinstall) self.view.layoutifneeded() }, completion: nil) }
// expanded, constraints example:
go any-any size class , see constraint of them gayed out means not active here active in different size classes, example any-regular.
// expanded
to set initial orientation can override viewwillappear , install/uninstall right constraints:
override func viewwillappear(animated: bool) { super.viewwillappear(animated) let transitiontolandscape = view.frame.size.width > view.frame.size.height let constraintstouninstall = transitiontolandscape ? portraitconstraints : landscapeconstraints let constraintstoinstall = transitiontolandscape ? landscapeconstraints : portraitconstraints view.layoutifneeded() nslayoutconstraint.deactivateconstraints(constraintstouninstall) nslayoutconstraint.activateconstraints(constraintstoinstall) }
Comments
Post a Comment