ios 如何在其他子视图下方插入子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10051514/
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-30 17:45:06  来源:igfitidea点击:

How can I insert a subview below the other subviews

iossubviewaddsubview

提问by jxdwinter

everyone.

每个人。

I have some labels that I draw them in the xib file, and add a background view using codes,but the background view is in the front of these labels, so I cant see them. So, my question is how can I add a background view below these labels.

我在xib文件中绘制了一些标签,并使用代码添加了背景视图,但背景视图在这些标签的前面,所以我看不到它们。所以,我的问题是如何在这些标签下方添加背景视图。

Thank you in advance.

先感谢您。

回答by Paul.s

If you Google UIViewyou'll end up at the documentation - UIView Class Reference

如果你谷歌UIView你最终会在文档 - UIView 类参考

Then you need to look down the list of methods until you find something that sounds like it will roughly fit your needs like these two:

然后,您需要查看方法列表,直到找到听起来大致符合您需求的方法,例如这两个:

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index

and

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview

At this point you just have to read the descriptions of how the methods behave and pick whichever one is easiest to implement for your design. It's really good for your own learning if you try to find the answer yourself before you ask a question

在这一点上,您只需要阅读方法行为的描述,然后选择最容易为您的设计实现的方法。如果你在提问之前尝试自己找到答案,这对你自己的学习真的很有好处

回答by sch

You can use the method insertSubview:atIndex:

您可以使用该方法 insertSubview:atIndex:

[self.view insertSubview:backgroundView atIndex:0];

回答by Kwaku Eshun

You can call this method in Swift 3

您可以在 Swift 3 中调用此方法

view.insertSubview(yourSubView, at: 0)

or

或者

view.sendSubview(toBack: yourSubView)

回答by QED

In IB the subviews of a view are listed in a hierarchy under that view. Subviews which appear above their siblings in that hierarchy will appear below them in the rendered view. So in the example below, the in99:99 AMlabel appears under the Sign Inbutton at runtime.

在 IB 中,视图的子视图在该视图下的层次结构中列出。在该层次结构中出现在其兄弟姐妹之上的子视图将在渲染视图中出现在它们之下。因此,在下面的示例中,in99:99 AM标签Sign In在运行时出现在按钮下方。

If your background is just a UIImageView, consider adding the view to your NIB and then setting its UIImage at runtime. That way, you don't get confused. Also, what Paul.s said.

如果您的背景只是一个 UIImageView,请考虑将视图添加到您的 NIB,然后在运行时设置其 UIImage。这样,您就不会感到困惑。另外,Paul.s 所说的

enter image description here

在此处输入图片说明

回答by Alfi

Swift 3 and later.

斯威夫特 3 及更高版本。

view.insertSubview(theViewYouWantToAdd, at: 0)

回答by rajeswari ratala

https://medium.com/swift-programming/dynamic-layouts-in-swift-b56cf8049b08

https://medium.com/swift-programming/dynamic-layouts-in-swift-b56cf8049b08

Use VerticalLayout as the parent. it is quite simple and effective

使用 VerticalLayout 作为父级。它非常简单有效