如何在Swift中向后发送UIView元素 [英] How to send a UIView element backwards in Swift
问题描述
所以我有一个我在main.Storyboard中添加的Button,以及View的主背景,然后在viewController.swift文件中,我创建了一个UIView矩形形状,我想要坐在该按钮后面(作为它的背景)。
So I have a Button which i have added in main.Storyboard, along with a main background for the View, and then in the viewController.swift file, I have created a UIView rectangle shape which I want to sit behind that button (as a background for it).
问题是,当我添加矩形UIView时,它总是将它添加到按钮前面。所以我在网上研究并找到了sendSubviewToBack代码。我添加了这个,但它一直发送到视图的主UIImage背景后面。
The problem is, when I added the rectangle UIView it always added it in front of the button. So I researched online and found the sendSubviewToBack code. I have added this in, but it sends it all the way behind the main UIImage background of the view.
有没有办法可以在按钮后面发送这个UIView但是在UIImage背景前面?
Is there a way i can just send this UIView behind the button but in front of the UIImage background?
func nextBarDisplayed() {
let theHeight = self.view.frame.height
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: theHeight - 50, width: self.view.frame.width, height: 50)
self.view.insertSubview(nextBar, aboveSubview: myBackgroundView)
}
推荐答案
来自 apple documentation :
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
你可以使用以下方式将你的酒吧带到前面:
You can bring your bar to the front using:
view.bringSubviewToFront(nextBar)
您使用以下方式将视图发送到酒吧后面:
Your send your view to the back of the bar using:
nextBar.view.sendSubviewToBack(myView)
这篇关于如何在Swift中向后发送UIView元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!