Advertisement
Don_Mag

Untitled

Jul 24th, 2023
1,791
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.28 KB | None | 0 0
  1. class BasicPickerVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  2.    
  3.     let picker = UIPickerView()
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         self.view.addSubview(self.picker)
  9.  
  10.         // set frame near top-left
  11.         picker.frame = .init(x: 60.0, y: 80.0, width: 240.0, height: 160.0)
  12.        
  13.         // we don't want any resizing o ndevice rotation
  14.         picker.autoresizingMask = []
  15.        
  16.         // so we can see its frame
  17.         picker.backgroundColor = .yellow
  18.  
  19.         self.picker.delegate = self
  20.         self.picker.dataSource = self
  21.     }
  22.    
  23.     override func viewSafeAreaInsetsDidChange() {
  24.         super.viewSafeAreaInsetsDidChange()
  25.         //This gets called everytime the device rotates, causing the picker view to redraw and reload all components. Trying to avoid this method being called.
  26.        
  27.         print ("viewSafeAreaInsetsDidChange")
  28.         print (self.view.safeAreaInsets)
  29.     }
  30.  
  31.     func numberOfComponents(in pickerView: UIPickerView) -> Int {
  32.         print("num components: 1")
  33.         return 1
  34.     }
  35.    
  36.     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  37.         print("num rows: 30")
  38.         return 30
  39.     }
  40.    
  41.     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  42.         print("Title for Row:", row)
  43.         return "Row: \(row)"
  44.     }
  45.    
  46. }
  47.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement