简体   繁体   English

如何在iOS中制作自定义按钮?

[英]How to make custom buttons in iOS?

默认的iOS UI很好,但是如果我想为按钮使用图像,那我该怎么做-是OpenGL吗?

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. 您需要使用图像编辑器查找或创建按钮。

UIButton has a lot of support for custom buttons. UIButton对自定义按钮有很多支持。 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. 然后,在其正下方,将“状态配置”设置为您想要自定义的状态(默认,选中等),并将“图像”属性设置为您想要的图像。

That image must be part of the project first before it will show up in the Image attribute (or in the app). 该图像必须首先成为项目的一部分,然后才能显示在Image属性(或应用程序)中。 You can add the image to the project simply by dragging it into the file navigator on the left side of the screen. 您只需将图像拖到屏幕左侧的文件导航器中,即可将其添加到项目中。

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状态的更多信息

You can simply use PNG files that you create yourself. 您可以简单地使用自己创建的PNG文件。 The UIButton class allows you to use custom images for all the different states of the application. 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/# documentation / 2ddrawing / concept / drawingprintingios / graphicsdrawingoverview / graphicsdrawingoverview.html

You can do it by setting image or backgroundImage properties of a UIButton . 您可以通过设置UIButton image或backgroundImage属性来实现。 If you want your button to look nice when the size changes, you can use stretchableImageWithLeftCapWidth:topCapHeight: method of UIImage , with an appropriate image. 如果您希望按钮在大小变化时看起来不错,可以使用UIImage stretchableImageWithLeftCapWidth:topCapHeight:方法和适当的图像。

You may also want to use a custom component if it satisfies your need. 如果满足您的需要,您可能还想使用自定义组件

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 更改这两件事。将“ type”圆形矩形更改为custom并拖动图像以投影并选择它作为背景。运行并运行

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. 我已经看了所有的视频教程并阅读了一些书,但没有任何帮助,但是此站点及其四本书非常棒,并且对阅读和理解它很有帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM