繁体   English   中英

如何在iOS 10中布置设备轮换的子视图

[英]How to layout subviews on device rotation in iOS 10

如何在iOS 10中布置设备轮换的子视图?

我想在旋转设备时重绘我的子视图。

添加并使用didRotateFromInterfaceOrientation方向委托方法。

要布局子视图,请使用UIView autoresizingMask属性,

喜欢,

YourView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UIView文档

当视图的边界发生变化时,该视图会根据每个子视图的自动调整蒙版自动调整其子视图的大小。

autoresizingMask更改为None以外的其他值将触发layoutSubviews

因此,您可以使用下面的代码使用didRotateFromInterfaceOrientation委托方法来布局子视图。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

   [self.view layoutIfNeeded];
   [self.view setNeedsLayout];
   self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

   //....Other code
}


-(void)layoutSubviews{

   //write your code to draw here
}

由于didRotateFromInterfaceOrientation已在iOS 9中弃用,因此您应该使用viewWillTransition(_:_:)方法。

这是一个例子:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: nil, completion: { _ in 
        // called right after rotation transition ends
        self.view.setNeedsLayout()
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM