如何在 iOS 中制作自定义按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7984500/
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
How to make custom buttons in iOS?
提问by neutrino
The default iOS UI is nice but if I wanted to use images for buttons instead how would I do that - is it OpenGL?
默认的 iOS UI 很好,但如果我想使用图像作为按钮,我该怎么做 - 它是 OpenGL 吗?
回答by jemmons
UIButton has a lot of support for custom buttons. In the nib (or storyboard or whatever), just click on the button and set its "Type" to "Custom" in the attribute inspector on the right side of the screen. Then, just below that, set the State Config to the sate you'd like (default, selected, etc) to customize, and set Image attribute to the image you'd like.
UIButton 对自定义按钮有很多支持。在笔尖(或故事板或其他任何东西)中,只需单击按钮并在屏幕右侧的属性检查器中将其“类型”设置为“自定义”。然后,在其下方,将 State Config 设置为您想要自定义的状态(默认、选定等),并将 Image 属性设置为您想要的图像。
That image must be part of the project first before it will show up in the Image attribute (or in the app). You can add the image to the project simply by dragging it into the file navigator on the left side of the screen.
该图像必须首先成为项目的一部分,然后才能显示在 Image 属性(或应用程序)中。只需将图像拖到屏幕左侧的文件导航器中,即可将图像添加到项目中。
回答by Randall
Make the button a "custom button" type in interface builder. Then set the background image to be whatever you want the background to be.
使按钮成为界面构建器中的“自定义按钮”类型。然后将背景图像设置为您想要的任何背景。
Unfortunately the default button style is a little boring. You'll need to find or make a button with an image editor.
不幸的是,默认按钮样式有点乏味。您需要使用图像编辑器查找或制作按钮。
回答by 5StringRyan
You can use images for buttons using a few simple lines of code:
您可以使用几行简单的代码为按钮使用图像:
// code to set image for button at normal state
[myButton setImage:[UIImage imageNamed:@"buttonImage.png"]
forState: UIControlStateNormal];
You can also use Xcode to change the type to "Custom" then you can set images for each button "state."
您还可以使用 Xcode 将类型更改为“自定义”,然后您可以为每个按钮“状态”设置图像。
You can read more about UIButton states here
您可以在此处阅读有关 UIButton 状态的更多信息
回答by Eduardo Scoz
You can simply use PNG files that you create yourself. The UIButton class allows you to use custom images for all the different states of the application.
您可以简单地使用自己创建的 PNG 文件。UIButton 类允许您为应用程序的所有不同状态使用自定义图像。
You don't really need to do custom rendering yourself, but if you really want, you can use UIKit to draw the images yourself in code (no OpenGL involved): http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/graphicsdrawingoverview/graphicsdrawingoverview.html
你真的不需要自己做自定义渲染,但如果你真的想要,你可以使用 UIKit 在代码中自己绘制图像(不涉及 OpenGL):http: //developer.apple.com/library/ios/#文档/2ddrawing/conceptual/drawingprintingios/graphicsdrawingoverview/graphicsdrawingoverview.html
回答by Ahmet Ardal
You can do it by setting image or backgroundImage properties of a UIButton
.
If you want your button to look nice when the size changes, you can use stretchableImageWithLeftCapWidth:topCapHeight:
method of UIImage
, with an appropriate image.
您可以通过设置 a 的 image 或 backgroundImage 属性来实现UIButton
。如果您希望按钮在大小更改时看起来不错,可以使用 ,stretchableImageWithLeftCapWidth:topCapHeight:
方法和UIImage
适当的图像。
You may also want to use a custom componentif it satisfies your need.
如果满足您的需要,您可能还想使用自定义组件。
回答by Tunvir Rahman Tusher
its very easy
这很容易
change this two thing.Change "type" round rectangle to custom and drag image to project and select it as background .Run and Go
改变这两件事。将“类型”圆角矩形更改为自定义并将图像拖动到项目并将其选择为背景。运行并运行
回答by ashim888
Well so far i was too looking for using custom buttons and the graphics, I've come through this site they have some awesome tutorial which was quite helpful for me. I've watched all the video tutorial and read some books but nothing helped me but this site and their 4 books are quite awesome and really helpful to read and understand it well.
到目前为止,我一直在寻找使用自定义按钮和图形的方法,我浏览了这个网站,他们有一些很棒的教程,对我很有帮助。我已经观看了所有的视频教程并阅读了一些书籍,但没有任何帮助,但是这个网站和他们的 4 本书非常棒,非常有助于阅读和理解它。
http://www.raywenderlich.com/27191/learn-to-code-ios-apps-4-making-it-beautiful
http://www.raywenderlich.com/27191/learn-to-code-ios-apps-4-making-it-beautiful