Create custom buttons for Interface Builder
Does anyone know how to create custom buttons for Interface Builder? Like instead of have just a regular Round Rect Button, I wa开发者_JAVA技巧nt to have like a custom 3D button and some random image background for that button. How to do this?
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 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 then let IB know about it.
Update based on OP's comment
You can use -setImage:forState:
to supply your custom image for the given states.
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.
Then, in Interface Builder, change the class of the object you've subclassesed to your new class, and everything should work correctly.
Relevant reading:
- How to override -drawrect in UIButton subclass?
- http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
- http://www.cocoabuilder.com/archive/cocoa/284622-how-to-subclass-uibutton.html (If you really need to subclass UIButton instead)
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
If you want anything more fancy it's probably time to start Photoshop ;-)
精彩评论