iOS原生UISlider两边有空隙的解决方法

林承悦
2023-12-01

iOS原生UISlider两边有空隙,或者要改变slider的高度。写一个类继承于UISlider,重写里面的方法。

@interface MyCustomSlider : UISlider

@end
#import "MyCustomSlider.h"

@implementation MyCustomSlider

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//改变UIslide高度
//- (CGRect)trackRectForBounds:(CGRect)bounds{
//    bounds.origin.x = bounds.origin.x;
//    bounds.origin.y = (bounds.size.height-20)/2;
//    bounds.size.height = 20;
//    bounds.size.width = bounds.size.width;
//    return bounds;
//}
//- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds
//{
//      NSLog(@"bounds1:%@",NSStringFromCGRect(bounds));
//    return bounds;
//}
//- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds
//{
//      NSLog(@"bounds2:%@",NSStringFromCGRect(bounds));
//    return bounds;
//}
//两边有空隙,修改方法
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
    rect.origin.x = rect.origin.x - 5 ;
    rect.size.width = rect.size.width +10;
    return CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], 5 , 5);
}
@end


 类似资料: