gpt4 book ai didi

ios - UISlider 扭曲圆角

转载 作者:行者123 更新时间:2023-12-01 16:16:01 24 4
gpt4 key购买 nike

我创建了一个 UISlider,然后为最小轨道设置色调颜色,如下所示:

    [slider setMinimumTrackTintColor:[UIColor whiteColor]];

但是,我最终会在左侧出现这样的扭曲圆角。我该怎么办 ?

enter image description here

编辑:这是重现问题的代码:
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(40, 40, 180, 50)];

[slider setMinimumTrackTintColor:[UIColor whiteColor]];

最佳答案

enter image description here

我在增加 slider 高度时遇到了同样的错误。

当 slider 的高度增加时, slider 的边缘失去了圆角。为了修复它,我创建了带有圆角的图像并将其分配给 slider 最小轨道图像。

swift

 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);
}

在自定义 slider 内
 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;

}

关于ios - UISlider 扭曲圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26729402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com