2011-09-24 22 views
3

是否可以将图像添加到UISwitch背景,例如当状态为ON(是一个背景)和状态为OFF(另一个背景图像)时?UISwitch定制?

+0

http://stackoverflow.com/questions/12400239/uiswitch-with-custom-image 希望帮助:) – StackBuddy

回答

2

要更改背景颜色(而不是图像),您只需在下面执行操作。这会更改领域内所有UISwitch控件的关闭颜色。

[[UISwitch appearance] setTintColor:[UIColor brownColor]];//Off Color 
_locationSwitch.onTintColor = [UIColor orangeColor];//On Color 

如果要使用图像,请使用以下使用分段控件的酷炫技巧。 https://stackoverflow.com/a/5088099/1705353

如果你想写很多代码,你可以编写自己的控件。瓦列里·凡指出:https://github.com/homick/iPhone-Snippets/tree/master/General

也是另一个资源是:http://www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch的代码在:http://cdn2.raywenderlich.com/wp-content/uploads/2012/10/CustomSwitchResources.zip

0

您可以通过继承UIControl重新创建一个自定义UISwitch。通过这样做,您可以完全控制交换机的外观。你可以看看SevenSwitch。我创建的自定义UISwitch替代品。开/关颜色可以根据您的喜好进行定制。

https://github.com/bvogelzang/SevenSwitch