简体   繁体   English

在方向更改为自动布局中的横向时隐藏视图

[英]Hiding a view when orientation changes to landscape in autolayouts

I am creating a sample application in which i am copying Facebook screens in order to practice auto layouts. 我正在创建一个示例应用程序,我在其中复制Facebook屏幕以练习自动布局。 When I run login screen in portrait mode, it looks perfect. 当我以纵向模式运行登录屏幕时,它看起来很完美。

The problem is as soon as the orientation changes to landscape, all the views collapse because of header image, as shown here 问题是,一旦方向更改为横向,所有视图都会因标题图像而崩溃, 如此处所示

What i want is that, in landscape mode, header image disappears so that other views get its space. 我想要的是,在横向模式下,标题图像消失,以便其他视图获得其空间。 I don't want to use scrollview. 我不想使用scrollview。

I tried this: 我试过这个:

headerImageView.isHidden = true

But the result came out to be this The imageview got disappeared but didn't leave its space. 但结果却出现了这种情况 。图像视图消失了,但没有留下它的空间。 Can anyone suggest me a good solution? 谁能建议我一个好的解决方案?

Ps Sorry for the images being this way because of my reputation. 因为我的声誉,对不起这些图像。

When using Auto Layout you can leverage Size Classes. 使用自动布局时,您可以使用大小类。 See description below or example here: https://github.com/jonaszmclaren/AutolayoutExample 请参阅下面的说明或示例: https//github.com/jonaszmclaren/AutolayoutExample

Set image view for compact width and height (wC hC - iPhone in landscape) and for wR hC (iPhone Plus in landscape) to not installed: 设置紧凑宽度和高度的图像视图(wC hC - 横向iPhone)和未安装的wR hC(横向iPhone版):

在此输入图像描述

Constraint between text field and image view not enabled for wC hC and wR hC: wC hC和wR hC未启用文本字段和图像视图之间的约束:

在此输入图像描述

And finally for wC hC and wR hC you have to define text fields's top contraint - I did it to the top of the view. 最后,对于wC hC和wR hC,你必须定义文本字段的顶部约束 - 我将它放在视图的顶部。

在此输入图像描述

This way, image view for portait will be visible and text view pinned to image view, and in landscape image view will be hidden and text field pinned to top of the view. 这样,portait的图像视图将可见,文本视图固定到图像视图,并且将隐藏横向图像视图,并将文本字段固定到视图的顶部。

The best way is to use scrollView in such type of scenarios. 最好的方法是在这种类型的场景中使用scrollView。 If you don't want to have the scrollView, then you must give the bottom constraint for last button, and set the priority low of that particular constraint. 如果您不想拥有scrollView,则必须为最后一个按钮赋予底部约束,并将该特定约束的优先级设置为低。 It will work fine for current screen(both landscape and portrait), but when you'll go for small screen ie 4s or 5, then purpose of auto layout will fail. 它适用于当前屏幕(横向和纵向),但是当你选择小屏幕即4s或5时,自动布局的目的将失败。

像我一样

If you hide the image than it will only not show to user But Space will be used by Image on screen. 如果隐藏图像而不向用户显示,但屏幕上的图像将使用Space。 Better Approach is you can set the Height Of Image 0 when orientation change to Landscape. 更好的方法是,当方向更改为横向时,您可以设置图像高度0。 You can create the Outlet of Height Constraint of Image and Change it according to Orientation.This method is called before orintation change. 您可以创建图像的高度约束出口并根据方向更改它。此方法在orintation更改之前调用。 You need to Create outlat of Height constraint of Image. 您需要创建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 will just changed the visibility of the view . isHidden只会改变视图的可见性 It will not remove it from that position. 它不会将其从该位置移除。 To solve this issue create a outlet of height constraint of header view and changed it to 0 on orientation change. 要解决此问题,请创建标题视图的高度约束出口,并在方向更改时将其更改为0。

ex: 例如:

headerViewHeightConstraint.constant = 0.0
self.view.layoutIfNeeded()

and to restore it on portrait mode set height again. 并再次以纵向模式设置高度恢复它。

headerViewHeightConstraint.constant = // height value which you want to set
self.view.layoutIfNeeded()

Another option could be to place your view inside a stack view. 另一种选择可能是将视图放在堆栈视图中。 Then hiding the headerImageView should recover the unused space. 然后隐藏headerImageView应该恢复未使用的空间。

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

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