如何在 swift 中更改除弹出视图之外的总屏幕背景颜色

How to change total screen background colour except popup view in swift

我无法创建单独的自定义 popview.. 所以我尝试了下面提到的答案之一

这是故事板视图层次结构

 popBG view background colour is black with alpha = 0.3

这是代码:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var popViewtop: UIView!    
@IBOutlet weak var testTable: UITableView!

@IBOutlet weak var popView: UIView!
@IBOutlet weak var popBG: UIView!
override func viewDidLoad() {
    super.viewDidLoad()
    popViewtop.isHidden = true
  
}

@IBAction func testBtn(_ sender: Any) {
    popViewtop.isHidden = false
}

@IBAction func btnPop(_ sender: Any) {
    
     let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
    popViewtop.isHidden = true

    self.navigationController?.pushViewController(viewController, animated: true);
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

}

现在 tableview 和按钮没有透明显示..只有 popbg 和 popview 来了..我错过了什么吗..

实际上我需要这样:一些深色的总背景视图和白色高亮的弹出视图

使您的弹出视图全屏显示..它有背景并在该背景上有作为弹出窗口的子视图...因此您在显示或隐藏弹出窗口后无需更改任何内容并且易于实现...全屏UIView 上面有 popUp UIView

您应该有一个名为 MainPopUpView 的视图...在此视图中您将添加两个 UIView...

  1. 背景视图 ... 黑色,alpha 0.3
  2. PopUpUI 视图 ...显示实际弹出窗口
                -> backGround UIView full screen  (with alpha 0.3) 
   MainPopUpView 
               -> popUpView short & centre and shows actual content

所以 MainPopUpView 有两个视图 ...

这是层次结构

  1. 如果为查看提供 alpha 值,

view的subView的alpha值变化,包括popupView

  1. 如果给视图背景色,包括popupView

view的subView的alpha值不变,包括popupView


popupViewview 的子视图,view 的 alpha 值影响其子视图的 alpha。

view.addSubview(popupView)

您当前的结构:

RootView->Subviews //Changing RootView alpha effects Subviews.

解决方案是 popupView 不是 view 的子视图,您使用

显示颜色变化

需要容器视图与 popupView

分开
// backgroundColorChangeContainerView add other views ( tableView ... )
view.addSubview(backgroundColorChangeContainerView)
view.addSubview(popupView)

其他实现最终图像的方式.. custom pop controller avoids this situation

将您的 popupView 放入自定义 viewController,然后

 @IBAction func addAddressBtn(_ sender: Any) {
       present(PopupViewController(nibName: "PopupViewController", bundle: nil), animated: true) {}
 }