UISlider手柄可在不同尺寸的iPad上改变宽度 [英] UISlider Handle Changes Width on Different Size iPads

查看:101
本文介绍了UISlider手柄可在不同尺寸的iPad上改变宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个垂直滑块,它使用以下代码在其自定义类中生成一个句柄图像:

  func generateHandleImage(带颜色:UIColor) - > UIImage {


let rect = CGRect(x:0,y:0,width:(superview?.bounds.size.height)!* 0.07,
height :( superview?.bounds.size.width)!)


返回
中的UIGraphicsImageRenderer(size:rect.size).image {(imageContext)imageContext.cgContext.setFillColor(color .cgColor)
imageContext.cgContext.fill(rect.insetBy(dx:0,dy:0))
}
}

我在滑块中为IB设置了自动布局约束,使其在x&它的超视图的y轴和相等的宽度和高度与它的超视图的高度和宽度(因为它是一个垂直滑块)。



这是使我的滑块垂直并调用函数生成句柄图像的代码:

  //使滑块垂直
let sliderTransform = CGAffineTransform(rotationAngle:CGFloat(M_PI_2)+ CGFloat(M_PI))
pitchBendSlider.transform = sliderTransform

pitchBendSlider.setThumbImage(pitchBendSlider.generateHandleImage(with:.white),for:。normal)

I希望句柄图像等于其超视图的宽度。它在iOS模拟器中的iPad Air 2上正确执行此操作,但在iPad Pro 12.9上它大约是superview宽度的80-90%,而在iPad Air上它略大于superview的宽度。



如何在所有iPad尺寸上使自定义滑块的图像手柄的宽度等于其超视图的宽度?

解决方案

通过试验和错误我发现这是一个时间问题。句柄图像rect在 viewDidLoad 中过早绘制。



我的黑客解决方案是将绘制滑块手柄的函数放入按钮动作函数中,该函数调用要显示的视图(其中有垂直滑块)。 / p>

添加时间延迟可能会解决同样的问题。两者都是黑客解决方案,我仍然不了解为什么在<之前无法识别正确的屏幕尺寸code> viewDidLoad 在VC中调用cont ains垂直滑块......



然而,这解决了这个问题。


I have a vertical slider which uses the following code to generate a handle image within its custom class:

func generateHandleImage(with color: UIColor) -> UIImage {


    let rect = CGRect(x: 0, y: 0, width: (superview?.bounds.size.height)! * 0.07,
                      height: (superview?.bounds.size.width)!)


    return UIGraphicsImageRenderer(size: rect.size).image { (imageContext) in
        imageContext.cgContext.setFillColor(color.cgColor)
        imageContext.cgContext.fill(rect.insetBy(dx: 0, dy: 0))
    }
}

I have set the autolayout constraints in IB for the slider for it to be centered in the x & y axes of it's superview and equal widths and heights with the heights and widths of it's superview (since it is a vertical slider).

this is the code which makes my slider vertical and calls the function to generate the handle image:

// Make Sliders Vertical
    let sliderTransform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2) + CGFloat(M_PI))
    pitchBendSlider.transform = sliderTransform

    pitchBendSlider.setThumbImage(pitchBendSlider.generateHandleImage(with: .white), for: .normal)

I want the handle image to be equal to the width of it's superview. It does this correctly on an iPad Air 2 in iOS simulator, but on an iPad Pro 12.9" it is about 80-90% of the superview's width, and on an iPad Air it is slightly bigger than the superview's width.

How can I make the width of the custom slider's image handle equal to it's superview's width on all iPad sizes?

解决方案

Through trial & error I discovered this was a "timing issue". The handle image rect was being drawn too soon in viewDidLoad.

My "hack" solution was to place the function that draws the slider handle into the button action function which calls the views to appear (which has the vertical slider inside it).

Adding a timing delay would probably solve the problem just the same. Both are hack solutions, and I still lack the understanding of why the proper screen size is not recognized prior to viewDidLoad being called in the VC which contains the vertical slider...

However, this solved the problem.

这篇关于UISlider手柄可在不同尺寸的iPad上改变宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆