Swift
Swift

Reputation: 1182

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

I cant create saperate custom popview.. so I have tried like below mentioned one of the answer

here is storyboard view hierarchy

enter image description here

 popBG view background colour is black with alpha = 0.3

this is code:

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
}

}

now the tableview and button are not showing transperently.. only popbg and popview coming.. did i miss anything..

enter image description here

Actually i need like this: total backgroundview in some darkcolour and popupview heighlighted in white colour

enter image description here

Upvotes: 1

Views: 1594

Answers (3)

Jawad Ali
Jawad Ali

Reputation: 14417

Make your Popup view full screen ..which have background and have subview as popUp over that background ...so you dont need to change anything once you show or hide popup and its easy to implement... Full screen UIView having popUp UIView over it

You should have a view called it MainPopUpView ... in this view you will add two UIViews ...

  1. Background View ... with black color with alpha 0.3
  2. PopUpUI View ... which shows actual popup
                -> backGround UIView full screen  (with alpha 0.3) 
   MainPopUpView 
               -> popUpView short & centre and shows actual content

So MainPopUpView have two views ...

here is the hierarchy

enter image description here

Upvotes: 1

dengST30
dengST30

Reputation: 4037

other way to achieve like your final image.. custom pop controller avoids this situation

put your popupView in a custom viewController, then

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

Upvotes: 1

dengApro
dengApro

Reputation: 4038

  1. if giving alpha value to view,

view's subView's alpha value changes, including popupView

  1. if giving background color to view, including popupView

view's subView's alpha value does not change, including popupView


popupView is a subView of view,view's alpha value affects its subviews's alpha.

view.addSubview(popupView)

Your current structure:

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

The solution is that the popupView is not a subview of view , which you show color changing with

Need a container view to separate from popupView

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

Upvotes: 2

Related Questions