0

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.

Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
  • [Does this answer your question?](https://stackoverflow.com/questions/25483414/how-to-override-trait-collection-for-initial-uiviewcontroller-with-storyboard) – Sweeper Sep 03 '20 at 03:48
  • Hi sweeper. The answer in the link you provided is basically variant of my second solution. – Prajeet Shrestha Sep 03 '20 at 04:31
  • That does not make your question not-a-duplicate. – Sweeper Sep 03 '20 at 04:33
  • My question is: Can I safely ignore the log message with first solution? Is there any other way to vary traitCollection for view controller. I didn't find any similar question or answer to it. – Prajeet Shrestha Sep 03 '20 at 05:40
  • You should not ignore the warning. Otherwise the warning wouldn't be there. You should use one of the ways mentioned in the duplicate target to override trait collections. If you are looking for a way other than the 7 answers in the duplicate target, you can place a bounty on the duplicate target, to see if anyone knows any other way. "I found this duplicate, but I don't think any of its answers is the optimal solution", on its own, is not an excuse for posting a duplicate question. – Sweeper Sep 03 '20 at 05:52
  • "Can I safely ignore the log message with first solution?" This is a unique question in itself and it has lot of context along with it. And "You should not ignore the warning. Otherwise the warning wouldn't be there. " the answer like this is not why we are here in stackoverflow. What are the implications of the warning message, and other elaboration is expected? If no one knows the answer leave the question as it is. Rather than closing it with inconclusive answer in comment section. – Prajeet Shrestha Sep 03 '20 at 06:03

0 Answers0