[英]Hiding a view when orientation changes to landscape in autolayouts
使用自动布局时,您可以使用大小类。 请参阅下面的说明或示例: https : //github.com/jonaszmclaren/AutolayoutExample
设置紧凑宽度和高度的图像视图(wC hC - 横向iPhone)和未安装的wR hC(横向iPhone版):
wC hC和wR hC未启用文本字段和图像视图之间的约束:
最后,对于wC hC和wR hC,你必须定义文本字段的顶部约束 - 我将它放在视图的顶部。
这样,portait的图像视图将可见,文本视图固定到图像视图,并且将隐藏横向图像视图,并将文本字段固定到视图的顶部。
如果隐藏图像而不向用户显示,但屏幕上的图像将使用Space。 更好的方法是,当方向更改为横向时,您可以设置图像高度0。 您可以创建图像的高度约束出口并根据方向更改它。此方法在orintation更改之前调用。 您需要创建Image的高度约束。
@IBOutlet var heightConstraint : NSLayoutConstraint!
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval)
{
if toInterfaceOrientation == .landscapeLeft || toInterfaceOrientation == .landscapeRight{
// Imageview height constraint outlate
heightConstraint.constant = 0
}
else{
heightConstraint.constant = 100
}
}
isHidden只会改变视图的可见性 。 它不会将其从该位置移除。 要解决此问题,请创建标题视图的高度约束出口,并在方向更改时将其更改为0。
例如:
headerViewHeightConstraint.constant = 0.0
self.view.layoutIfNeeded()
并再次以纵向模式设置高度恢复它。
headerViewHeightConstraint.constant = // height value which you want to set
self.view.layoutIfNeeded()
另一种选择可能是将视图放在堆栈视图中。 然后隐藏headerImageView应该恢复未使用的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.