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
Navigation bar button image not showing in Swift
提问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。但是,它只是在设计中以及在我运行应用程序时显示了这一点。它只是在导航区域显示蓝色污点。
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.
这是我一直在尝试添加的图像和我目前拥有的设置。
Settings:
设置:
回答by Nicholas Murray
回答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)