Advertisement
Don_Mag

Transparent Modal VC

Feb 25th, 2025
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.33 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ShowTransparentViewController: UIViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         view.backgroundColor = .white
  9.        
  10.         var cfg = UIButton.Configuration.filled()
  11.         cfg.title = "Show Transparent VC"
  12.        
  13.         let btn = UIButton(configuration: cfg, primaryAction: UIAction() { _ in
  14.             self.presentTransparentVC()
  15.         })
  16.  
  17.         btn.translatesAutoresizingMaskIntoConstraints = false
  18.         view.addSubview(btn)
  19.        
  20.         let g = view.safeAreaLayoutGuide
  21.        
  22.         NSLayoutConstraint.activate([
  23.             btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 60.0),
  24.             btn.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  25.         ])
  26.     }
  27.    
  28.     func presentTransparentVC() {
  29.         let vc = TransparentViewController()
  30.         vc.modalPresentationStyle = .overFullScreen
  31.        
  32.         // personal choice... I find .crossDissolve nicer for full-screen modals
  33.         vc.modalTransitionStyle = .crossDissolve
  34.        
  35.         present(vc, animated: true, completion: nil)
  36.     }
  37. }
  38.  
  39. class TransparentViewController: UIViewController {
  40.    
  41.     override func viewDidLoad() {
  42.         super.viewDidLoad()
  43.        
  44.         // Set background color to black with 50% alpa
  45.         view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
  46.  
  47.         // "bottom" view with rounded corners
  48.         let bottomView = UIView()
  49.         bottomView.backgroundColor = .init(red: 0.95, green: 0.95, blue: 0.975, alpha: 1.0)
  50.         bottomView.layer.cornerRadius = 32.0
  51.        
  52.         // "X" close button
  53.         var cfg = UIButton.Configuration.plain()
  54.         cfg.title = "X"
  55.  
  56.         let btn = UIButton(configuration: cfg, primaryAction: UIAction() { _ in
  57.             self.dismiss(animated: true)
  58.         })
  59.        
  60.         btn.translatesAutoresizingMaskIntoConstraints = false
  61.         bottomView.addSubview(btn)
  62.  
  63.         bottomView.translatesAutoresizingMaskIntoConstraints = false
  64.         view.addSubview(bottomView)
  65.        
  66.         let g = view.safeAreaLayoutGuide
  67.  
  68.         NSLayoutConstraint.activate([
  69.            
  70.             bottomView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 40.0),
  71.             bottomView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
  72.             bottomView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
  73.             bottomView.heightAnchor.constraint(equalToConstant: 280.0),
  74.            
  75.             btn.topAnchor.constraint(equalTo: bottomView.topAnchor, constant: 20.0),
  76.             btn.trailingAnchor.constraint(equalTo: bottomView.trailingAnchor, constant: -20.0),
  77.            
  78.         ])
  79.        
  80.        
  81.     }
  82.  
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement