override UITraitCollection of a UIView
Asked Answered
M

0

10

I have a UITabBar which has to have a traitCollection with compact horizontalSizeClass. Because on a regular horizontalSizeClass, UITabBar renders TabBarIcon and Title side by side.

I did subclass UITabBar and overriden the traitCollection getter to give me modified traitCollection.

override var traitCollection: UITraitCollection{
    let current = super.traitCollection
    let compact = UITraitCollection(horizontalSizeClass: .compact)
    return UITraitCollection(traitsFrom: [current, compact])
}

This does the job but, I started getting below warning

Class MyTabBar overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.

After checking Apple Documentation I did find one API, overrideTraitCollection(forChild:) But the viewController which holds my UITabBar needs to have regular horizontalSizeClass.

I also found a similar question but it doesn't answer my question of overriding the traitCollection of a UIView.

I don't want to override the traitCollection of a UIViewController but only the UIView.

What is the right way to override traitCollection of a UIView?

Monopetalous answered 12/12, 2019 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.