Don_Mag icon

Transparent Modal VC

Don_Mag | PRO | 02/25/25 12:19:16 PM UTC | 0 ⭐ | 8257 👁️ | Never ⏰ | []
Swift |

2.33 KB

|

None

|

0 👍

/

0 👎

import UIKit
 
class ShowTransparentViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        var cfg = UIButton.Configuration.filled()
        cfg.title = "Show Transparent VC"
        
        let btn = UIButton(configuration: cfg, primaryAction: UIAction() { _ in
            self.presentTransparentVC()
        })
 
        btn.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(btn)
        
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([
            btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 60.0),
            btn.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        ])
    }
    
    func presentTransparentVC() {
        let vc = TransparentViewController()
        vc.modalPresentationStyle = .overFullScreen
        
        // personal choice... I find .crossDissolve nicer for full-screen modals
        vc.modalTransitionStyle = .crossDissolve
        
        present(vc, animated: true, completion: nil)
    }
}
 
class TransparentViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set background color to black with 50% alpa
        view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
 
        // "bottom" view with rounded corners
        let bottomView = UIView()
        bottomView.backgroundColor = .init(red: 0.95, green: 0.95, blue: 0.975, alpha: 1.0)
        bottomView.layer.cornerRadius = 32.0
        
        // "X" close button
        var cfg = UIButton.Configuration.plain()
        cfg.title = "X"
 
        let btn = UIButton(configuration: cfg, primaryAction: UIAction() { _ in
            self.dismiss(animated: true)
        })
        
        btn.translatesAutoresizingMaskIntoConstraints = false
        bottomView.addSubview(btn)
 
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bottomView)
        
        let g = view.safeAreaLayoutGuide
 
        NSLayoutConstraint.activate([
            
            bottomView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 40.0),
            bottomView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
            bottomView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
            bottomView.heightAnchor.constraint(equalToConstant: 280.0),
            
            btn.topAnchor.constraint(equalTo: bottomView.topAnchor, constant: 20.0),
            btn.trailingAnchor.constraint(equalTo: bottomView.trailingAnchor, constant: -20.0),
            
        ])
        
        
    }
 
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎