2017-12-27 234 views
3

一套图标和颜色,我想要实现这样的事情(不带红点):迅速 - iOS设备 - 上UISwitch

enter image description here

我得到什么:

enter image description here

我已经尝试了上面的代码片段,但图片重复了几次,而不是只显示一次。

myswitch.thumbTintColor = UIColor(patternImage:UIImage(named:"myimage")!) 
+0

尝试使用此https://github.com/plavatvornica/CustomUISwitch – Shezad

回答

0

https://medium.com/@milenko_52829/making-custom-uiswitch-part-1-cc3ab9c0b05b

您可以参考制作自己的定制UISwitch不依赖于第三方库本教程。

在该教程中,在setupUI()函数中,它们将自定义视图分配为thumbView。所以在这个函数来创建一个UIImageView(说myThumbImageView),你的图像添加到ImageView的和作为一个子视图添加的ImageView到self.thumbView

所以,你的代码将

func setupUI() { 
    self.clear() 
    self.clipsToBounds = false 
    self.thumbView.backgroundColor = self.thumbTintColor 
    self.thumbView.isUserInteractionEnabled = false 

    let imageName = "yourImage.png" 
    let image = UIImage(named: imageName) 
    let myThumbImageView = UIImageView(image: image!) 
    myThumbImageView.frame = CGRect(x: 0, y: 0, width: self.thumbView.bounds.size.height, height: self.thumbView.bounds.height) 
    self.thumbView.addSubview(self.myThumbImageView) 

    self.addSubview(self.thumbView) 
}