Don_Mag icon

Untitled

Don_Mag | PRO | 07/25/23 11:21:01 AM UTC | 0 ⭐ | 8288 👁️ | Never ⏰ | []
Swift |

1.73 KB

|

None

|

0 👍

/

0 👎

class ParentVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemYellow
 
        let childVC = ChildVC()
        self.addChild(childVC)
        childVC.view.frame = .init(x: 60.0, y: 80.0, width: 240.0, height: 160.0)
        childVC.view.autoresizingMask = []
        self.view.addSubview(childVC.view)
        childVC.didMove(toParent: self)
    }
    
}
 
class ChildVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    
    let picker = UIPickerView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(self.picker)
        
        self.picker.delegate = self
        self.picker.dataSource = self
 
    }
    
    override func viewDidAppear(_ animated: Bool) {
        print(#function)
        super.viewDidAppear(animated)
        
        if self.view.superview != nil {
            picker.frame = self.view.bounds
            picker.autoresizingMask = []
        } else {
            picker.frame = .init(x: 20.0, y: 100.0, width: 260.0, height: 160.0)
        }
 
        picker.backgroundColor = .yellow
    }
    
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        //This gets called everytime the device rotates, causing the picker view to redraw and reload all components. Trying to avoid this method being called.
        
        print ("viewSafeAreaInsetsDidChange")
        print (self.view.safeAreaInsets)
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        print(#function)
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        print(#function)
        return 30
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        print(#function, "Row:", row)
        return "Row: \(row)"
    }
    
}
 

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

    |

    👍

    /

    👎