Apple WWDC视频"使应用程序自适应,第2部分" (参见https://developer.apple.com/videos/play/wwdc2016/233/)大约14:30分钟标记使用方法UINavigationBar。 forTraitCollection()进行描述。从该视频中,可以看到使用该方法的一些代码:
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)
}
我找不到有关forTraitCollection()的文档。我一直在搜索Apple开发人员文档和Web。你能告诉我去哪儿吗?
答案 0 :(得分:1)
视频中的代码已“进化”。目前正确的工作代码(iOS 10,swift 3)是:
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)
}
感谢rmaddy让我走上正轨。