ios 如何使用 swift 显示和/或隐藏子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27975321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:22:37  来源:igfitidea点击:

How do I show and/or hide a subview using swift

iosswiftuiview

提问by jammyman34

So I've created a ViewControl in my storyboard that has 3 subviews. Each one represents a different view I want to show depending on which table row was selected on the previous screen (NavControl). I start with all of the subviews hidden via the Attributes Inspector's 'hidden' attribute being checked. All of the objects within each of these views are NOT hidden, but are being hidden because the subview itself is hidden (obviously). Thinking I could use the tag attribute I've given each of the three subviews a tag (0, 1 and 2), but can't figure out how to use that either (just in case this is useful as providing me with an option of how to do this I wanted to mention it here).

所以我在我的故事板中创建了一个 ViewControl,它有 3 个子视图。每个代表我想显示的不同视图,具体取决于在前一个屏幕 (NavControl) 上选择了哪个表格行。我从通过检查属性检查器的“隐藏”属性隐藏的所有子视图开始。每个视图中的所有对象都不是隐藏的,而是隐藏的,因为子视图本身是隐藏的(显然)。想我可以使用标签属性,我已经给三个子视图中的每一个一个标签(0、1 和 2),但也无法弄清楚如何使用它(以防万一这对我提供了一个选项有用如何做到这一点我想在这里提到它)。

So, how the heck do I show and then hide any of these subviews? I don't want to go through each object in a subview and toggle its hidden property to true/false I feel like I should just be able to 'show/hide' the entire subview. thus achieving the same result, but much more succinctly.

那么,我到底如何显示然后隐藏这些子视图中的任何一个?我不想遍历子视图中的每个对象并将其隐藏属性切换为 true/false 我觉得我应该能够“显示/隐藏”整个子视图。从而达到相同的结果,但更简洁。

I can't find anything that will help me via web searches or stackoverflow searches.

我找不到任何可以通过网络搜索或 stackoverflow 搜索帮助我的东西。

My code is very simple. I capture the row that was selected in the previous screen and pass it to a variable on the details screen that contains the subviews. I know this is working because I've set up println()'s on the details screens viewDidLoad function. So now all I have to do is going into each of these conditions and tell it which subview to show and/or hide.

我的代码很简单。我捕获在上一个屏幕中选择的行并将其传递给包含子视图的详细信息屏幕上的变量。我知道这是有效的,因为我已经在详细信息屏幕 viewDidLoad 函数上设置了 println()。所以现在我所要做的就是进入这些条件中的每一个并告诉它显示和/或隐藏哪个子视图。

Thanks I appreciate all of this communities help! I'd be lost without it.

谢谢 我感谢所有这些社区的帮助!没有它我会迷路的。

回答by iHulk

Use this to hide a view in swift

使用它来快速隐藏视图

viewVar.isHidden = true

回答by AdamPro13

You should create IBOutlets for each of the three subviews. Then you can show/hide each of them directly from those references. If you hide a view, it will automatically hide its subviews.

您应该IBOutlet为三个子视图中的每一个创建s。然后您可以直接从这些引用中显示/隐藏它们中的每一个。如果你隐藏一个视图,它会自动隐藏它的子视图。

Once you have an outlet for the view, you can do this: viewYouWantToHide.isHidden = true

一旦你有一个视图的出口,你可以这样做: viewYouWantToHide.isHidden = true

回答by Midhun MP

If you have tags for each view you can hide and display them using:

如果每个视图都有标签,则可以使用以下方法隐藏和显示它们:

Objective C

目标 C

For Hiding:

隐藏:

[[self.view viewWithTag:1] setHidden:YES];

Showing:

显示:

[[self.view viewWithTag:1] setHidden:NO];

In Swift:

在斯威夫特:

Hiding:

隐藏:

self.view.viewWithTag(1)?.isHidden = true

Showing:

显示:

self.view.viewWithTag(1)?.isHidden = false

NOTE:Replace 1with your tag value.

注意:替换1为您的标签值。

回答by Amr Angry

however the fact that isHidden is a naming convention for checking the status and is a getter method but despite that fact in swift we use it as setter and getter property

然而事实上 isHidden 是一个用于检查状态的命名约定并且是一个 getter 方法但是尽管在 swift 中我们使用它作为 setter 和 getter 属性

view.isHidden = true