Skip to content

Instantly share code, notes, and snippets.

@jawj
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save jawj/9037468b91ec98e47ba8 to your computer and use it in GitHub Desktop.

Select an option

Save jawj/9037468b91ec98e47ba8 to your computer and use it in GitHub Desktop.
A UISlider subclass for iOS7 that respects alignmentRectInsets on custom thumb images
@interface AlignmentRectRespectingSlider : UISlider
@end
@implementation AlignmentRectRespectingSlider
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
UIImage* image = self.currentThumbImage;
UIEdgeInsets insets = image.alignmentRectInsets;
CGFloat fullWidth = image.size.width;
CGFloat fullHeight = image.size.height;
CGFloat coreWidth = fullWidth - insets.left - insets.right;
CGFloat coreHeight = fullHeight - insets.top - insets.bottom;
CGFloat usableTrackWidth = rect.size.width - coreWidth;
CGFloat offset = usableTrackWidth * value;
CGRect thumbRect = CGRectMake(rect.origin.x - insets.left + offset,
rect.origin.y + 0.5f * rect.size.height - 0.5f * coreHeight - insets.top,
fullWidth, fullHeight);
return thumbRect;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment