我创建了一个UISlider,然后为min track设置色调颜色,如下所示:
[slider setMinimumTrackTintColor:[UIColor whiteColor]];
但是我最后在左侧扭曲了圆角,就像这样。我该怎么办?
编辑:以下是重现问题的代码:
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 40, 180, 50)];
[slider setMinimumTrackTintColor:[UIColor whiteColor]];
答案 0 :(得分:1)
在增加滑块的高度时遇到了相同的错误。
当滑块的高度增加时,滑块的边缘将失去圆角。为了解决这个问题,我创建了带有圆角的图像,并将其分配给滑块的最小轨道图像。
快速
override func viewDidLoad() {
super.viewDidLoad()
slider.maximumTrackTintColor = UIColor.gray;
let img:UIImage = GetImage(rectToDraw: CGRect(x: 0, y: 0, width: 400, height: 400));
slider.setMinimumTrackImage(img.resizableImage(withCapInsets: UIEdgeInsets(top: 13,left: 15,bottom: 15,right: 14)), for: UIControlState.normal)
slider.setMinimumTrackImage(img.resizableImage(withCapInsets: UIEdgeInsets(top: 13,left: 15,bottom: 15,right: 14)), for: UIControlState.selected)
}
func GetImage(rectToDraw:CGRect) -> UIImage {
let layer:CALayer = CALayer()
layer.frame = rectToDraw;
layer.cornerRadius = 20;
layer.backgroundColor = UIColor.red.cgColor;
UIGraphicsBeginImageContext(layer.frame.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image :UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image;
}
Xamarin.iOS
在自定义渲染器中
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
MySlideriOS sliderIos = new MySlideriOS();
SetNativeControl(sliderIos);
base.OnElementChanged(e);
}
在“自定义”滑块中
public class MySlideriOS : UISlider
{
public UILabel label = null;
public MySlideriOS()
{
this.MaximumTrackTintColor = UIColor.Gray;
UIImage img = GetImage(new CGRect(0,0,400,400));
this.SetMinTrackImage(img.CreateResizableImage(new UIEdgeInsets(13, 15, 15,14)), UIControlState.Normal);
this.SetMinTrackImage(img.CreateResizableImage(new UIEdgeInsets(13, 15, 15, 14)), UIControlState.Selected);
this.MaxValue = 15;
}
public UIImage GetImage(CGRect recttoDraw)
{
CGRect rect = recttoDraw;
CALayer layer = new CALayer();
layer.Frame = recttoDraw;
layer.CornerRadius = 20;
layer.BackgroundColor = UIColor.Red.CGColor;
UIGraphics.BeginImageContext(layer.Frame.Size);
layer.RenderInContext(UIGraphics.GetCurrentContext());
UIImage image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
答案 1 :(得分:-1)
我遇到了同样的故障,并根据颜色为滑块添加了一个minimalTrackImage找到了解决方案。
UIImage *black = [UIImage imageWithColor:[UIColor blackColor]];
[self.slider setMinimumTrackImage:black forState:UIControlStateNormal];
使用以下方法添加一个UIImage类别 - imageWithColor
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0, 1.0);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}