pbm
pbm

Reputation: 5371

Cannot find apple documentation about forTraitCollection

The Apple WWDC video "Making Apps Adaptive, Part 2" (see https://developer.apple.com/videos/play/wwdc2016/233/) at about the 14:30 minute mark describes using the method UINavigationBar.forTraitCollection(). From that video, here's some code that uses that method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let verticalCompactTrait = UITraitCollection(verticalSizeClass: .compact)
    let compactAppearance = UINavigationBar.forTraitCollection(verticalCompactTrait)
    compactAppearance.setBackgroundImage(nil, for: .default)

    let verticalRegularTrait = UITraitCollection(verticalSizeClass: .regular)
    let verticalAppearance = UINavigationBar.forTraitCollection(verticalRegularTrait)
    verticalAppearance.setBackgroundImage(UIImage(), for: .default)
}

I cannot find documentation about forTraitCollection(). I've searched Apple developer documentation and the web in general. Can you please tell me where to look?

Upvotes: 2

Views: 81

Answers (1)

pbm
pbm

Reputation: 5371

The code given in the video has "evolved". The correct working code at the moment (iOS 10, swift 3) is:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let verticalCompactTrait = UITraitCollection(verticalSizeClass: .compact)
    let compactAppearance = UINavigationBar.appearance(for: verticalCompactTrait)
    compactAppearance.setBackgroundImage(nil, for: .default)

    let verticalRegularTrait = UITraitCollection(verticalSizeClass: .regular)
    let regularAppearance = UINavigationBar.appearance(for: verticalRegularTrait)
    let navBgImage:UIImage = UIImage(named: "someImage.jpg")!
    regularAppearance.setBackgroundImage(navBgImage, for: .default)
}

Thanks to rmaddy for putting me on the right track.

Upvotes: 2

Related Questions