xcode 导航栏按钮图像未在 Swift 中显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32827704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:57:23  来源:igfitidea点击:

Navigation bar button image not showing in Swift

iosxcodeswiftuibarbuttonitem

提问by beninabox_uk

I'm back again.

我又回来了。

I'm writing an app in Swift using Xcode 7. I've added a navigation controller and on top of that a navigation item, and then a bar button. I'm trying to change the image of the bar button to a png that I've imported into Xcode. However, it just shows this in the design and also when I run the app. It just shows a blue smudge in the navigation area.

我正在使用 Xcode 7 在 Swift 中编写一个应用程序。我添加了一个导航控制器和一个导航项,然后是一个条形按钮。我正在尝试将条形按钮的图像更改为我已导入 Xcode 的 png。但是,它只是在设计中以及在我运行应用程序时显示了这一点。它只是在导航区域显示蓝色污点。

bar button image not appearing!

条形按钮图像没有出现!

I've tried importing the image into the project and also into images.xcassets with the same results.

我已经尝试将图像导入到项目中,也导入到 images.xcassets 中,结果相同。

Anyone know what may be wrong here?

有谁知道这里可能有什么问题?

Here's the image I've been trying to add and the settings I currently have.

这是我一直在尝试添加的图像和我目前拥有的设置。

image I'm trying to add

我正在尝试添加的图片

Settings:

设置:

settings of image

图像设置

回答by Nicholas Murray

The way to resolve navigation button images appearing as blue is to select the image set in Assets.

解决导航按钮图像显示为蓝色的方法是选择资产中设置的图像。

And then choose Original Image from the Render As select options.

然后从“渲染为”选择选项中选择“原始图像”。

enter image description here

在此处输入图片说明

回答by cekisakurek

You should have a image with alpha.(The sample image you posted has white background). Also you should set the image with template mode. I dont know how to do that in Interface builder. This is a sample code to convert the image to template.

您应该有一张带有 alpha 的图像。(您发布的示例图像具有白色背景)。您还应该使用模板模式设置图像。我不知道如何在界面构建器中做到这一点。这是将图像转换为模板的示例代码。

var image = UIImage(named: "").imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)