iOS:UIToolbar setBackgroundImage无效

Sal*_*ieh 1 uitoolbar ios

我无法更改uitoolbar背景图片.给出错误.

这个navigationBar代码正常工作.

UIImage *navBackgroundImage = [UIImage imageNamed:@"bartop.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

但是这个工具栏代码不起作用并且出错.

UIImage *navBackgroundImage = [UIImage imageNamed:@"bartop.png"];
[[UIToolbar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

我哪里错了?


更新:

这段代码有效:

UIImage *testimage = [[UIImage imageNamed:@"bartop"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];
Run Code Online (Sandbox Code Playgroud)

los*_*sit 6

根据文档,您需要使用UIToolbar的方法是

- setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIToolbarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics
Run Code Online (Sandbox Code Playgroud)

您正在使用

- setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
Run Code Online (Sandbox Code Playgroud)

尝试使用它,看看它是否适合你.

更多信息在这里.