放置 UIView 覆盖 TabBar 和 NavBar
Placing UIView To Cover TabBar & NavBar
我正在尝试制作一个具有透明背景(另一个 UIView
)的弹出窗口 (UIView
)。 'popup UIView
' 一切正常,但我不知道如何使用 'transparent background UIView
'(在 NavigationBar 和 TabBar 上方)。
首先我在情节提要中创建了 UIView 并连接了插座:
popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y);
self.view.addSubview(popupView)
popupView.clipsToBounds = true
popupView.alpha = 0
然后,在显示时 popupView
我正在创建透明背景 UIView:
func clicked() {
self.popupView.alpha = 1
let screenSize: CGRect = UIScreen.mainScreen().bounds
let opaqueView = UIView()
opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height)
opaqueView.backgroundColor = UIColor.blackColor()
opaqueView.alpha = 0.5
self.view.addSubview(opaqueView)
}
但是,背景视图不会覆盖 NavigationBar 或 TabBar。我试过了,但没有任何变化:
myTabBar.view.bringSubviewToFront(opaqueView)
我想要实现的是,虽然在最前面有 popup UIView
,但在包括 NavBar 和 TabBar 在内的所有内容上都有 opaque UIView
,但在 popup UIView
后面
更新:
关于@Alex 的回答,通过这个块,我实现了在 TabBar 和 NavBar 上显示 opaqueView
;但现在它也在 popupView 之上。
func display() {
popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y);
self.view.addSubview(popupView)
popupView.clipsToBounds = true
let opaqueView = UIView()
let screenSize: CGRect = UIScreen.mainScreen().bounds
opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height)
UIApplication.sharedApplication().keyWindow!.insertSubview(opaqueView, belowSubview: popupView)
}
如何将 opaqueView 置于 popupView 之下,而 opaqueView 位于其他一切之上?
试试这个:
UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(opaqueView)
更新 Swift 4.2
UIApplication.shared.keyWindow!.bringSubview(toFront: opaqueView!)
更新 Swift 5
UIApplication.shared.keyWindow!.bringSubviewToFront(opaqueView!)
func addButtonTapped(){
if self.transparentBackground == nil{
self.transparentBackground = UIView(frame: UIScreen.main.bounds)
self.transparentBackground.backgroundColor = UIColor(white: 0.0, alpha: 0.54)
UIApplication.shared.keyWindow!.addSubview(self.transparentBackground)
self.opaqueView = self.setupOpaqueView()
self.transparentBackground.addSubview(opaqueView)
UIApplication.shared.keyWindow!.bringSubview(toFront: self.transparentBackground)
self.view.bringSubview(toFront: transparentBackground)
}
}
func setupOpaqueView() -> UIView{
let mainView = UIView(frame: CGRect(x: 16, y: 100, width: Int(UIScreen.main.bounds.width-32), height: 200))
mainView.backgroundColor = UIColor.white
let titleLabel = UILabel(frame: CGRect(x: 16, y: 20, width: Int(mainView.frame.width-32), height: 100))
titleLabel.text = "This is the opaque"
titleLabel.textAlignment = .center
titleLabel.font = font
titleLabel.textColor = UIColor(white: 0.0, alpha: 0.54)
mainView.addSubview(titleLabel)
let OKbutton = UIButton(frame: CGRect(x: 16, y: Int(mainView.frame.height-60), width: Int(mainView.frame.width-32), height: 45))
OKbutton.backgroundColor = UIColor(red: 40.0 / 255.0, green: 187.0 / 255.0, blue: 187.0 / 255.0, alpha: 1)
OKbutton.layer.cornerRadius = 10
mainView.addSubview(OKbutton)
OKbutton.setTitle("OK", for: .normal)
OKbutton.setTitleColor(UIColor.white, for: .normal)
OKbutton.titleLabel?.font = font
OKbutton.addTarget(self, action: #selector(FirstViewController.handleOKButtonTapped(_:)), for: .touchUpInside)
return mainView
}
func handleOKButtonTapped(_ sender: UIButton){
UIView.animate(withDuration: 0.3, animations: {
self.transparentBackground.alpha = 0
}) { done in
self.transparentBackground.removeFromSuperview()
self.transparentBackground = nil
}
}
要透明的视图,用这个
yourView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
(swift 3+) 这将使您的视图位于选项卡栏和导航栏的顶部
UIApplication.shared.keyWindow!.addSubview(yourView)
UIApplication.shared.keyWindow!.bringSubview(toFront: yourView)
最佳解决方案是:
tabBarController?.view.addSubview(view)
UIApplication.shared.keyWindow!.bringSubviewToFront(view: view) 在 iOS 13 之后贬值。让透明视图同时覆盖选项卡控制器和导航控制器的最佳方法是执行此操作。
if let tabBarController = self.tabBarController {
tabBarController.view.addSubview(view)
}
我正在尝试制作一个具有透明背景(另一个 UIView
)的弹出窗口 (UIView
)。 'popup UIView
' 一切正常,但我不知道如何使用 'transparent background UIView
'(在 NavigationBar 和 TabBar 上方)。
首先我在情节提要中创建了 UIView 并连接了插座:
popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y);
self.view.addSubview(popupView)
popupView.clipsToBounds = true
popupView.alpha = 0
然后,在显示时 popupView
我正在创建透明背景 UIView:
func clicked() {
self.popupView.alpha = 1
let screenSize: CGRect = UIScreen.mainScreen().bounds
let opaqueView = UIView()
opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height)
opaqueView.backgroundColor = UIColor.blackColor()
opaqueView.alpha = 0.5
self.view.addSubview(opaqueView)
}
但是,背景视图不会覆盖 NavigationBar 或 TabBar。我试过了,但没有任何变化:
myTabBar.view.bringSubviewToFront(opaqueView)
我想要实现的是,虽然在最前面有 popup UIView
,但在包括 NavBar 和 TabBar 在内的所有内容上都有 opaque UIView
,但在 popup UIView
更新:
关于@Alex 的回答,通过这个块,我实现了在 TabBar 和 NavBar 上显示 opaqueView
;但现在它也在 popupView 之上。
func display() {
popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y);
self.view.addSubview(popupView)
popupView.clipsToBounds = true
let opaqueView = UIView()
let screenSize: CGRect = UIScreen.mainScreen().bounds
opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height)
UIApplication.sharedApplication().keyWindow!.insertSubview(opaqueView, belowSubview: popupView)
}
如何将 opaqueView 置于 popupView 之下,而 opaqueView 位于其他一切之上?
试试这个:
UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(opaqueView)
更新 Swift 4.2
UIApplication.shared.keyWindow!.bringSubview(toFront: opaqueView!)
更新 Swift 5
UIApplication.shared.keyWindow!.bringSubviewToFront(opaqueView!)
func addButtonTapped(){
if self.transparentBackground == nil{
self.transparentBackground = UIView(frame: UIScreen.main.bounds)
self.transparentBackground.backgroundColor = UIColor(white: 0.0, alpha: 0.54)
UIApplication.shared.keyWindow!.addSubview(self.transparentBackground)
self.opaqueView = self.setupOpaqueView()
self.transparentBackground.addSubview(opaqueView)
UIApplication.shared.keyWindow!.bringSubview(toFront: self.transparentBackground)
self.view.bringSubview(toFront: transparentBackground)
}
}
func setupOpaqueView() -> UIView{
let mainView = UIView(frame: CGRect(x: 16, y: 100, width: Int(UIScreen.main.bounds.width-32), height: 200))
mainView.backgroundColor = UIColor.white
let titleLabel = UILabel(frame: CGRect(x: 16, y: 20, width: Int(mainView.frame.width-32), height: 100))
titleLabel.text = "This is the opaque"
titleLabel.textAlignment = .center
titleLabel.font = font
titleLabel.textColor = UIColor(white: 0.0, alpha: 0.54)
mainView.addSubview(titleLabel)
let OKbutton = UIButton(frame: CGRect(x: 16, y: Int(mainView.frame.height-60), width: Int(mainView.frame.width-32), height: 45))
OKbutton.backgroundColor = UIColor(red: 40.0 / 255.0, green: 187.0 / 255.0, blue: 187.0 / 255.0, alpha: 1)
OKbutton.layer.cornerRadius = 10
mainView.addSubview(OKbutton)
OKbutton.setTitle("OK", for: .normal)
OKbutton.setTitleColor(UIColor.white, for: .normal)
OKbutton.titleLabel?.font = font
OKbutton.addTarget(self, action: #selector(FirstViewController.handleOKButtonTapped(_:)), for: .touchUpInside)
return mainView
}
func handleOKButtonTapped(_ sender: UIButton){
UIView.animate(withDuration: 0.3, animations: {
self.transparentBackground.alpha = 0
}) { done in
self.transparentBackground.removeFromSuperview()
self.transparentBackground = nil
}
}
要透明的视图,用这个
yourView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
(swift 3+) 这将使您的视图位于选项卡栏和导航栏的顶部
UIApplication.shared.keyWindow!.addSubview(yourView)
UIApplication.shared.keyWindow!.bringSubview(toFront: yourView)
最佳解决方案是:
tabBarController?.view.addSubview(view)
UIApplication.shared.keyWindow!.bringSubviewToFront(view: view) 在 iOS 13 之后贬值。让透明视图同时覆盖选项卡控制器和导航控制器的最佳方法是执行此操作。
if let tabBarController = self.tabBarController {
tabBarController.view.addSubview(view)
}