I have a child-view controller that do not want to have system safe areas or viewSafeAreaInsetsDidChange() called on rotation. So far this is not working:
In Superview:
let childVC = UIViewController()
self.addChild(childVC)
childVC.view.frame = CGRect(x:0, y: self.view.bounds.height - 300, width: self.view.bounds.width, height: 300)
self.view.addSubview(childVC.view)
childVC.didMove(toParent: self)
In Child View Controller:
class childVC: UIViewController {
let picker = UIPickerView()
override final func loadView() {
super.loadView()
self.viewRespectsSystemMinimumLayoutMargins = false
self.view.insetsLayoutMarginsFromSafeArea = false
self.view.preservesSuperviewLayoutMargins. = false
}
override final func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Some mention this to be inside viewDidAppear
self.viewRespectsSystemMinimumLayoutMargins = false
self.view.insetsLayoutMarginsFromSafeArea = false
self.view.preservesSuperviewLayoutMargins = false
//Add picker view here
self.picker.delegate = self
self.picker.dataSource = self
self.picker.frame = self.view.bounds
self.view.addSubview(self.picker)
}
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)
}
}
Issue
Everytime the device rotates, viewSafeAreaInsetsDidChange() gets called causing the picker view to redraw and reload.
Goal
Trying to avoid viewSafeAreaInsetsDidChange() being called every time the device rotates.
viewSafeAreaInsetsDidChange()This is a notification call. You ignore it, or respond to it... but you cannot prevent the safe area insets from changing.
These two examples: https://pastebin.com/vLkNj6vy and https://pastebin.com/cZPTZ17C show that
viewSafeAreaInsetsDidChange()is called on device rotation, but the picker view does NOT reload.The picker view will reload if its frame is outside the affected safe-area change.
Here is a quick example:
Looks like this:
and rotating the device:
When that is run, we will see the
viewSafeAreaInsetsDidChange()being logged, but the picker view will NOT reload.However, in
viewDidLoad(), if we change the constraints to the view instead of the safe area:we've placed the picker view frame outside the safe area, and it will reload on rotation.
If you don't want the picker to reload (for some reason), you'll need to manage that via your dataSource / delegate funcs.