ios 将 UIButton 带到前层
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22789261/
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
Bring UIButton to front layer
提问by Ryan
I know there is a way of bringing a subview to the front of the hierarchy. However is there a way of bringing a button that i have placed on a story and calling a function that will make that button the top layer so its not hidden by any other elements that are added that aren't in the story board.
我知道有一种方法可以将子视图带到层次结构的前面。但是,有没有一种方法可以将我放置在故事上的按钮带入并调用一个函数,该函数将使该按钮成为顶层,因此它不会被添加到故事板中的任何其他元素隐藏。
Thanks,
谢谢,
回答by Ryan
I am late for this question, but not too late to answer it by Swift 3
我对这个问题迟到了,但 Swift 3 回答这个问题还为时不晚
Swift 3
斯威夫特 3
view.bringSubview(toFront: theButton)
回答by Gereon
A UIButton is a UIView, so you can call -bringSubviewToFront:
on your button instance.
UIButton 是一个 UIView,因此您可以调用-bringSubviewToFront:
您的按钮实例。
回答by dreamweiver
uiview
's are displayed in the order they are stacked. One can change this to say were to insert the new uiview
, which in your case is a uibutton.
uiview
的按堆叠顺序显示。可以将其更改为插入 new uiview
,在您的情况下是一个 uibutton 。
One way of doing that is by creating a IBOUtlet
of the uibutton
and adding that button instance as a subview/newview above an existing uiview
.
这样做的一种方式是通过创建IBOUtlet
的uibutton
和补充说按钮实例作为一个子视图/ NewView的现有以上uiview
。
CODE:
代码:
[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];
[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];
Also if you are not able to figure out the order of the uiview
, then you can check that in xcode
此外,如果您无法弄清楚 的顺序uiview
,那么您可以在xcode
- When running the project in debug mode under debug window there is a button(highlighted in below image) Debug View Hierarchy, you need to click that. (highlighted in below image)
- 在调试窗口下以调试模式运行项目时,有一个按钮(在下图中突出显示)Debug View Hierarchy,您需要单击它。(在下图中突出显示)
- After that you will able to see all the
views
rendering on the screen at current instance, using this feature you will be able to understand were is your newuiview
in theuiview stack order
(shown below).
- 之后,你将能看到所有
views
在屏幕上呈现在目前情况下,使用此功能,您将能够理解为是新uiview
的uiview stack order
(如下图所示)。
回答by Intentss
Swift
迅速
The following works great if the view you are working with is being hidden views that are in the same view.
如果您正在使用的视图是同一视图中的隐藏视图,则以下内容非常有用。
superview?.bringSubviewToFront(self)
回答by Dave Levy
Swift 5
斯威夫特 5
In my case I had to insert my view behind some buttons. By insert it at the zero position it was placed behind the buttons.
就我而言,我不得不在一些按钮后面插入我的视图。通过将其插入零位置,它被放置在按钮后面。
self.view.insertSubview(self.mapView!, at: 0)
回答by dimaskpz
i have the same problem with you and a friend of mine helped me
我和你有同样的问题,我的一个朋友帮助了我
i can bring my button to another subview using this function on your view controller where you declare your button.
我可以在您声明按钮的视图控制器上使用此函数将我的按钮带到另一个子视图。
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.keyWindow?.addSubview(chatButton)
}