简体   繁体   English

自定义UIButton的有效方法

[英]Efficient way to customize UIButtons

What is the best way to customize the look of UIButtons in an app? 在应用程序中自定义UIButton外观的最佳方法是什么? I am working on an app where we programatically update UIButtons to look the way we want. 我正在开发一个应用程序,我们以编程方式更新UIButton以查看我们想要的方式。 Most of the buttons are created in Interface Builder and we want to execute our customizeButton -method on them in an efficient way. 大多数按钮都是在Interface Builder中创建的,我们希望以高效的方式对它们执行customizeButton -method。 I only see two ways of achieving this: 我只看到两种方法来实现这一目标:

  1. Create the buttons in IB, assign them to an outlet and in code call customizeButton for each button. 在IB中创建按钮,将它们分配给插座,并在代码中为每个按钮调用customizeButton Problem: way too much hazzle 问题:太多的hazzle
  2. Subclass UIButton, in the constructor initWithCoder: call customizeButton . 子类UIButton,在构造函数initWithCoder:调用customizeButton Then use this class for buttons in IB. 然后将此类用于IB中的按钮。 Problem: allthough this works for buttons created in IB and requires less boilerplate code than point 1, I have read all the warnings against subclassing UIButton because of their class cluster. 问题:虽然这适用于在IB中创建的按钮,并且需要比第1点更少的样板代码,但我已经阅读了针对UIButton子类化的所有警告,因为它们的类集群。

Clearly there must be some better way, what is point 3? 显然必须有更好的方法,第3点是什么?

Have a look at UIAppearance. 看看UIAppearance。 It is available since iOS 5 and is especially for customizing the standard UI elements. 它自iOS 5开始提供,尤其适用于自定义标准UI元素。

For ever class that is customizable is a class method called +appearance: and you can use it like this. 对于可自定义的类,可以使用名为+appearance:的类方法+appearance:您可以像这样使用它。

[[UIButton appearance] setBackgroundColor:[UIColor redColor]];

Every button will have now a red background color. 每个按钮现在都有一个红色背景颜色。

To get started have a look at Ray Wenderlichs Tutorial User Interface Customization in iOS 5 and/or session 114 - Customizing the Appearance of UIKit Controls of the WWDC11 videos. 要开始,请查看iOS 5和/或会话114 - Customizing the Appearance of UIKit Controls中的Ray Wenderlichs教程用户界面自定义 114 - Customizing the Appearance of UIKit Controls WWDC11视频114 - Customizing the Appearance of UIKit Controls

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

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