设置UIToolBar背景图像swift

Har*_*nan 0 objective-c uitoolbar uibarmetrics uitoolbarposition swift

我正在使用swift来制作IOS应用程序.我有一个UIToolBar在我的应用程序中,我需要更改其背景.我在Objective-C中使用以下代码来做到这一点:

[topBar setBackgroundImage:[UIImage imageNamed:@"header_bg.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

现在,当我在swift中重写代码时,它看起来像这样:

topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: UIToolbarPositionAny, barMetrics: UIBarMetricsDefault)
Run Code Online (Sandbox Code Playgroud)

这显示的错误,Use of unresolved type UIBarMetricsDefaultUse of unresolved type UIToolbarPositionAny.所以我做了一个搜索并找到了这个文档.根据文档,我改变了这样的代码:

topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: Any, barMetrics: Default)
Run Code Online (Sandbox Code Playgroud)

但它仍然显示相同的错误.任何人都知道这里有什么问题吗?

Oxc*_*cug 6

在Swift中,枚举有点不同.例如,UIBarMetricsDefault你说,而不是说:.Default.所以,你的代码应该是这样的:

topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: .Any, barMetrics: .Default)
Run Code Online (Sandbox Code Playgroud)

我建议您查看http://developer.apple.com上的"Swift简介"文档,了解有关如何编写/使用枚举的更多信息.