Advertisement
Don_Mag

Untitled

Jul 25th, 2023
1,579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.73 KB | None | 0 0
  1. class ParentVC: UIViewController {
  2.    
  3.     override func viewDidLoad() {
  4.         super.viewDidLoad()
  5.         view.backgroundColor = .systemYellow
  6.  
  7.         let childVC = ChildVC()
  8.         self.addChild(childVC)
  9.         childVC.view.frame = .init(x: 60.0, y: 80.0, width: 240.0, height: 160.0)
  10.         childVC.view.autoresizingMask = []
  11.         self.view.addSubview(childVC.view)
  12.         childVC.didMove(toParent: self)
  13.     }
  14.    
  15. }
  16.  
  17. class ChildVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  18.    
  19.     let picker = UIPickerView()
  20.    
  21.     override func viewDidLoad() {
  22.         super.viewDidLoad()
  23.        
  24.         self.view.addSubview(self.picker)
  25.        
  26.         self.picker.delegate = self
  27.         self.picker.dataSource = self
  28.  
  29.     }
  30.    
  31.     override func viewDidAppear(_ animated: Bool) {
  32.         print(#function)
  33.         super.viewDidAppear(animated)
  34.        
  35.         if self.view.superview != nil {
  36.             picker.frame = self.view.bounds
  37.             picker.autoresizingMask = []
  38.         } else {
  39.             picker.frame = .init(x: 20.0, y: 100.0, width: 260.0, height: 160.0)
  40.         }
  41.  
  42.         picker.backgroundColor = .yellow
  43.     }
  44.    
  45.     override func viewSafeAreaInsetsDidChange() {
  46.         super.viewSafeAreaInsetsDidChange()
  47.         //This gets called everytime the device rotates, causing the picker view to redraw and reload all components. Trying to avoid this method being called.
  48.        
  49.         print ("viewSafeAreaInsetsDidChange")
  50.         print (self.view.safeAreaInsets)
  51.     }
  52.    
  53.     func numberOfComponents(in pickerView: UIPickerView) -> Int {
  54.         print(#function)
  55.         return 1
  56.     }
  57.    
  58.     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  59.         print(#function)
  60.         return 30
  61.     }
  62.    
  63.     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  64.         print(#function, "Row:", row)
  65.         return "Row: \(row)"
  66.     }
  67.    
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement