0

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

[topBar setBackgroundImage:[UIImage imageNamed:@"header_bg.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

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

topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: UIToolbarPositionAny, barMetrics: UIBarMetricsDefault)

这显示了Use of unresolved type UIBarMetricsDefault和的错误Use of unresolved type UIToolbarPositionAny。所以我进行了搜索并找到了这个文档。根据文档,我更改了这样的代码:

topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: Any, barMetrics: Default)

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

4

1 回答 1

6

枚举在 Swift 中的处理方式略有不同。例如,不是说:UIBarMetricsDefault,而是说.Default。因此,您的代码应如下所示:

topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: .Any, barMetrics: .Default)

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

于 2014-07-04T08:18:42.230 回答