Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 设置UIToolBar背景图像_Objective C_Swift_Uitoolbar_Uibarmetrics_Uitoolbarposition - Fatal编程技术网

Objective c 设置UIToolBar背景图像

Objective c 设置UIToolBar背景图像,objective-c,swift,uitoolbar,uibarmetrics,uitoolbarposition,Objective C,Swift,Uitoolbar,Uibarmetrics,Uitoolbarposition,我正在使用swift制作IOS应用程序。我的应用程序中有一个UIToolBar,我需要更改它的背景。我在Objective-C中使用以下代码来实现这一点: [topBar setBackgroundImage:[UIImage imageNamed:@"header_bg.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 现在,当我用swift重写代码时,它看起来是这样的: topBar

我正在使用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)
topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: Any, barMetrics: Default)
topBar.setBackgroundImage(UIImage(named:"header_bg.png"), forToolbarPosition: .Any, barMetrics: .Default)
这显示了
使用未解析类型UIBarMetricsDefault
使用未解析类型UIToolbarPositionAny
的错误。所以我做了一次搜索,找到了文档。根据文档,我更改了如下代码:

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

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

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

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

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

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

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

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

确切地说。。你说得对巧妙地。。你说得对D