2016-05-31 10 views
5

Sto tentando di creare un popup (UIView) con uno sfondo trasparente (altro UIView). Tutto sta funzionando bene per il 'popup UIView' ma non riuscivo a capire come portare 'sfondo trasparente UIView' (sopra NavigationBar e TabBar).Inserimento di UIView per copertura TabBar e NavBar

Per prima cosa ho creato l'UIView nello Storyboard e collegato alla presa:

popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y); 
self.view.addSubview(popupView) 
popupView.clipsToBounds = true 
popupView.alpha = 0 

Poi, durante la visualizzazione popupView sto creando lo sfondo trasparente 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) 
} 

Tuttavia, la vista sfondo doesn passare sopra NavigationBar o TabBar. Ho provato questo, ma non cambia nulla:

myTabBar.view.bringSubviewToFront(opaqueView) 

Quello che voglio ottenere è che, pur avendo popup UIView nella parte anteriore, avendo opaque UIView su tutto compreso NavBar e TabBar, ma dietro popup UIView


Aggiornamento:

Per quanto riguarda la risposta di @ Alex, con questo blocco, ho ottenuto la visualizzazione di opaqueView su TabBar & NavBa r; ma ora sta anche andando sopra la 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) 
} 

Come posso collocare opaqueView sotto popupView mentre opaqueView è sopra ogni altra cosa?

risposta

5

Prova questo:

UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(opaqueView) 
+0

Ho provato anche questo, sta portando l'opaqueView su popupView (che non voglio), e non lo porta su NavBar e TabBar. Sfortunatamente, la tua risposta non aiuta:/ – senty

+1

Per farlo funzionare dovresti aggiungere il tuo opaqueView in questo modo: UIApplication.sharedApplication(). KeyWindow! .addSubview (opaqueView); Quindi la visualizzazione opaca sarà nella parte superiore delle sottoview. Non è possibile aggiungerlo alla vista (self.view.addSubview (opaqueView)) e attendere la visualizzazione sulla barra di navigazione e sulla barra delle schede. Una delle altre soluzioni è aggiungerla a tabbarController.view o navigationController.view. –

+0

Funziona come un fascino. Molte grazie. Ma ho una domanda. Quando uso 'UIApplication.sharedApplication(). KeyWindow! .addSubview (opaqueView);' opaqueView passa a popUpView. Ho provato a usare 'opaqueView.bringSubviewToFront (popupView)', ma non cambia nulla. Come posso portare 'popUpView' sopra 'opaqueView'? – senty

1
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 

       } 
      } 

preview of the result

+0

Hey @ Hackman questa è un'ottima risposta! Come sarai in grado di applicare un UITapGesture a transparentBackground per chiudere la vista, proprio come fa il pulsante OK, ma invece un UITapGesture. Grazie mille per la tua risposta – juelizabeth

+1

Hey @juelizabeth, buona domanda. Per avere il tocco sulla vista trasparente, creare un UITapGestureRecognizer e aggiungerlo al metodo addButtonTapped. Basta aggiungere queste due linee a addButtonTapped 'lascia tap = UITapGestureRecognizer (target: self, action: #selector (FirstViewController.handleOKButtonTapped (_ :))) self.transparentBackground.addGestureRecognizer (tap)' – Hackman

+0

Grazie mille, questo davvero aiutato – juelizabeth

0

La vista che si vuole fare trasparente, utilizzare questo

yourView.backgroundColor = UIColor.black.withAlphaComponent (0.7)