How to make UISlider's “track” invisible?

2019-03-15 00:27发布

问题:

I'm trying to emulate Apple's "Slide to Unlock" feature in my application. I get to this point (image below), but as you can see the UISlider's "track" is visible and is covering up my text. Is there a way to change an attribute programmatically that will make the "track" invisible?

Please let me know if you need any of my code.

Thanks in advance!

EDIT: If I change the slider's alpha to 0, it gets rid of my sliding button, so doing that won't work unless I'm doing it wrong. :)

回答1:

here's an even easier way. No need to create images, just instantiate an empty UIImage class :P

UIImage *clearImage = [[UIImage alloc] init];
[self.slider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
[self.slider setMaximumTrackImage:clearImage forState:UIControlStateNormal];


回答2:

Actually I just figured it out. Here's what I did:

UIImage *sliderMinimum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[slider setMinimumTrackImage:sliderMinimum forState:UIControlStateNormal];
UIImage *sliderMaximum = [[UIImage imageNamed:@"clearTrack.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[slider setMaximumTrackImage:sliderMaximum forState:UIControlStateNormal];

clearTrack.png is just a clear slider image I made.

Now I have this: yay!



回答3:

There probably isn't a way to hide the track; the "slide to unlock" doesn't behave like a UISlider and is probably a custom control. You might be able to hack the slider control, maybe by setting opacity low (0 will make it hidden and it won't receive touches), but if you go that route you will probably have something break after an OS update. Apple doesn't bend over backwards for compatibility like Microsoft does.

The right way to do this is with a custom control. It may seem like more work than using a UISlider, but it's not if you compare it against all the time you have spent and/or will spend hacking a UISlider.

To do it: subclass UIControl. Write your own drawing code to make it look right (you can probably reuse some of whatever you are doing now). Then register for touch events to move the slider handle:

  • UIControlEventTouchDown: if it's on the handle, set a "moving" flag
  • UIControlEventTouchDragInside: if the moving flag is set, move the handle to the touch position; you can just update an instance variable, call setNeedsDisplay to draw it in the new position.
  • UIControlEventTouchUpInside: if moving flag is set, and handle is at end, unlock

If you want to mimic the real unlock handle, play around with it and see how it behaves. You might need to respond to the events differently (what happens if you drag outside the slider path). But you should be able to get the gist of it.



回答4:

In Swift:

slider.setMinimumTrackImage(UIImage(), forState: .Normal)
slider.setMaximumTrackImage(UIImage(), forState: .Normal)


回答5:

In answer to the stated question, you can set a transparent 1px png for the minimum and maximum track images.



回答6:

Looks like just setting the tint color to clear does it...

    [slider setMinimumTrackTintColor:[UIColor clearColor]];
    [slider setMaximumTrackTintColor:[UIColor clearColor]];


回答7:

Answer by @Arjay Waran is the best in my opinion.

Heres the Swift 3.0 Version of it.

slider.minimumTrackTintColor = UIColor.clear
slider.maximumTrackTintColor = UIColor.clear

Cheers



回答8:

From Story Board:-

select clear color for Min Track and Max Track