rmp*_*251 100 orientation uiinterfaceorientation ios landscape-portrait ios8
有人可以告诉我在iOS 8中使用纵向和横向界面方向的"正确"或"最佳"方法吗?似乎我想要用于此目的的所有功能都在iOS 8中被弃用,而我的研究似乎没有明确,优雅的替代方案.我真的应该看看宽度和高度,以确定我们是在纵向还是横向模式?
例如,在我的视图控制器中,我应该如何实现以下伪代码?
if we are rotating from portrait to landscape then
do portrait things
else if we are rotating from landscape to portrait then
do landscape things
Run Code Online (Sandbox Code Playgroud)
smi*_*org 254
Apple建议使用大小类作为可用屏幕空间大小的粗略度量,以便UI可以显着改变其布局和外观.考虑到纵向的iPad具有与横向相同的尺寸等级(常规宽度,常规高度).这意味着您的UI在两个方向之间应该或多或少相似.
但是,iPad中从纵向到横向的变化非常重要,即使尺寸类没有改变,您也可能需要对UI进行一些较小的调整.由于UIViewController不推荐使用与接口方向相关的方法,Apple现在建议在UIViewController替换时实现以下新方法:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
// Code here will execute before the rotation begins.
// Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Place code here to perform animations during the rotation.
// You can pass nil or leave this block empty if not necessary.
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Code here will execute after the rotation has finished.
// Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:]
}];
}
Run Code Online (Sandbox Code Playgroud)
大!现在你在旋转开始之前以及完成之后得到回调.但实际上知道旋转是纵向还是横向呢?
Apple建议将旋转视为父视图大小的变化.换句话说,在iPad从纵向旋转到横向旋转期间,您可以将其视为根级视图,只需将其bounds.size从更改{768, 1024}为{1024, 768}.知道了这一点,你应该使用size传递给viewWillTransitionToSize:withTransitionCoordinator:上面的方法来确定你是否正在旋转到纵向或横向.
如果您想要一种更加无缝的方式将遗留代码迁移到新的iOS 8工作方式,请考虑在UIView上使用这个简单类别,它可用于确定视图是基于其的"纵向"还是"横向"尺寸.
回顾一下:
viewWillTransitionToSize:withTransitionCoordinator:UIViewController中的回调.statusBarOrientation- 这基本上是设备级属性 - 来确定是否为"肖像"与"横向"布局视图.状态栏方向只应由处理UIWindow实际位于应用程序根本级别的事务的代码使用.CMo*_*ont 16
基于smileyborg非常详细(和接受)的答案,这是使用swift 3的改编:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil, completion: {
_ in
self.collectionView.collectionViewLayout.invalidateLayout()
})
}
Run Code Online (Sandbox Code Playgroud)
并在UICollectionViewDelegateFlowLayout实施中,
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// retrieve the updated bounds
let itemWidth = collectionView.bounds.width
let itemHeight = collectionView.bounds.height
// do whatever you need to do to adapt to the new size
}
Run Code Online (Sandbox Code Playgroud)
inV*_*ble 11
我只是使用通知中心:
添加一个方向变量(将在最后解释)
//Above viewdidload
var orientations:UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
Run Code Online (Sandbox Code Playgroud)
视图出现时添加通知
override func viewDidAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)
视图消失后删除通知
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceOrientationDidChangeNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)
触发通知时获取当前方向
func orientationChanged (notification: NSNotification) {
adjustViewsForOrientation(UIApplication.sharedApplication().statusBarOrientation)
}
Run Code Online (Sandbox Code Playgroud)
检查方向(纵向/横向)并处理事件
func adjustViewsForOrientation(orientation: UIInterfaceOrientation) {
if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
{
if(orientation != orientations) {
println("Portrait")
//Do Rotation stuff here
orientations = orientation
}
}
else if (orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight)
{
if(orientation != orientations) {
println("Landscape")
//Do Rotation stuff here
orientations = orientation
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加方向变量的原因是因为在物理设备上进行测试时,方向通知会在设备中的每次轻微移动时调用,而不是在旋转时调用.添加var和if语句只会在切换到相反方向时调用代码.
| 归档时间: |
|
| 查看次数: |
66544 次 |
| 最近记录: |