xcode 为 Interface Builder 创建自定义按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5094379/
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-14 20:18:21  来源:igfitidea点击:

Create custom buttons for Interface Builder

cocoa-touchxcodeios4buttoninterface-builder

提问by PatrickGamboa

Hey guys, Does anyone know how to create custom buttons for Interface Builder? Like instead of have just a regular Round Rect Button, I want to have like a custom 3D button and some random image background for that button, how anyone can tell me how to do this, thanks

嘿伙计们,有谁知道如何为 Interface Builder 创建自定义按钮?就像不是只有一个普通的圆形矩形按钮,我想要一个自定义的 3D 按钮和该按钮的一些随机图像背景,谁能告诉我如何做到这一点,谢谢

采纳答案by Joshua Nozzi

You will either need to find a third-party class (ideally with an Interface Builder plug-in so you can see it live in the IB file) or subclass UIButton or NSButton/NSButtonCell for Mac and provide your own 3D rainbow unicorn drawing behavior. :-)

您将需要找到第三方类(最好带有 Interface Builder 插件,以便您可以在 IB 文件中看到它)或子类 UIButton 或 NSButton/NSButtonCell for Mac 并提供您自己的 3D 彩虹独角兽绘图行为。:-)

Interface Builder can only show you classes it knows about - you can't add behavior / modify existing drawing behavior there because that's the wrong tool for the job. You'll need to find someone else's or subclass your own in code thenlet IB know about it.

Interface Builder 只能向您显示它知道的类 - 您不能在那里添加行为/修改现有的绘图行为,因为那是工作的错误工具。您需要在代码中找到其他人的或您自己的子类,然后让 IB 知道。

Update based on OP's comment

根据 OP 的评论更新

You can use -setImage:forState:to supply your custom image for the given states.

您可以使用-setImage:forState:为给定状态提供自定义图像。

回答by Dominic

To do it in Interface Builder is prohibitively complicated (writing an Interface Builder plugin is a non-trivial task). However, you can subclass UIControl (which is just a UIView) and define your custom drawing in the subclass.

在 Interface Builder 中执行此操作非常复杂(编写 Interface Builder 插件是一项重要的任务)。但是,您可以继承 UIControl(它只是一个 UIView)并在子类中定义您的自定义绘图。

Then, in Interface Builder, change the class of the object you've subclassesed to your new class, and everything should work correctly.

然后,在 Interface Builder 中,将您子类化的对象的类更改为新类,一切都应该正常工作。

Relevant reading:

相关阅读:

回答by nylund

It's probably easiest to use an image as the background for the button. For example you can use this tool to easily generate buttons with gradients, http://itunes.apple.com/us/app/uibutton-builder/id408204223?mt=8

使用图像作为按钮的背景可能是最简单的。例如,您可以使用此工具轻松生成带有渐变的按钮,http://itunes.apple.com/us/app/uibutton-builder/id408204223?mt=8

If you want anything more fancy it's probably time to start Photoshop ;-)

如果你想要更奇特的东西,可能是时候开始使用 Photoshop 了 ;-)