By default traitCollection of iPad in portrait or landscape mode is: Width: Regular, Height: Regular
Goal: I want to use Width: Compact and height:Regular traitCollection for iPad portrait mode.
Tried Solution:
override var traitCollection: UITraitCollection {
if view.bounds.width < view.bounds.height {
let traitCollection = [UITraitCollection(horizontalSizeClass: .compact), UITraitCollection(verticalSizeClass: .regular)]
return UITraitCollection(traitsFrom: traitCollection)
} else {
let traitCollection = [UITraitCollection(horizontalSizeClass: .unspecified), UITraitCollection(verticalSizeClass: .unspecified)]
return UITraitCollection(traitsFrom: traitCollection)
}
}
Problem: The above code seems to be working fine and my UI changes according to constraints specific to corresponding trait. But I get following log message in debugger:
Class SizeClassDemo.ViewController overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.
Second Possible Solution:
override func overrideTraitCollection(forChild childViewController: UIViewController) -> UITraitCollection? {
if view.bounds.width < view.bounds.height {
let traitCollection = [UITraitCollection(horizontalSizeClass: .compact), UITraitCollection(verticalSizeClass: .regular)]
return UITraitCollection(traitsFrom: traitCollection)
} else {
let traitCollection = [UITraitCollection(horizontalSizeClass: .unspecified), UITraitCollection(verticalSizeClass: .unspecified)]
return UITraitCollection(traitsFrom: traitCollection)
}
}
Problem with second solution:
So for this to work, I need to put this code on parent view controller. So if I don't have parent view controller for example in login screen, I need to put login screen in container view controller then put this method in parent view controller's class, this is not optimum solution.
Question:
Can I safely ignore the log message with first solution? Is there any other way to vary traitCollection for view controller.