所以我在 main.Storyboard 中添加了一个按钮,以及 View 的主背景,然后在 viewController.swift 文件中,我创建了一个 UIView 矩形形状,我想将其放在该按钮后面(作为它的背景)。
问题是,当我添加矩形 UIView 时,它总是将它添加到按钮前面。所以我在网上研究并找到了 sendSubviewToBack 代码。我已经添加了它,但它一直将它发送到 View 的主要 UIImage 背景后面。
有没有一种方法可以将这个 UIView 发送到按钮后面但在 UIImage 背景前面?
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:)
您可以使用以下方法将栏置于最前面:
view.bringSubviewToFront(nextBar)
您使用以下方式将您的 View 发送到栏的后面:
nextBar.view.sendSubviewToBack(myView)
关于ios - 如何在 Swift 中向后发送 UIView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769052/