How to make UISlider default thumb to be smaller like the ones in the iOS control center

后端 未结 5 543
孤独总比滥情好
孤独总比滥情好 2021-02-01 06:50

I\'m working on an app and I have a custom UISlider.
However, I\'m having some issues on how to make the default thumb to appear smaller like t

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 07:40

    You can't change the size of the default thumb image, but UISlider has a method setThumbImage(_:for:) that will allow you to pass a similar, smaller image.

    In your view controller viewDidLoad :

    let image:UIImage? = // ...
    yourSlider.setThumbImage(image, for: .normal)
    yourSlider.setThumbImage(image, for: .highlighted) // Also change the image when dragging the slider
    

    See Customizing the Slider’s Appearance of the API Reference.


    On iOS10, the default thumb image appear to be no more than a bordered white circle with a thin shadow dropped under (if you don't set the thumbTintColor).

    I use this snippet to generate a similar image that can be scaled down ;)

    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var data = " \
     \
     \
    	
    \ \
    \
    \
    \
    \ "; var DOMURL = self.URL || self.webkitURL || self; var img = new Image(); var svg = new Blob([data], { type: "image/svg+xml;charset=utf-8" }); var url = DOMURL.createObjectURL(svg); img.onload = function() { ctx.drawImage(img, 0, 0); DOMURL.revokeObjectURL(url); }; img.src = url;

提交回复
热议问题