[英]How to resize a view across all screen sizes?
I am creating a simple timer app and want to resize the timer circle to the size of the view with the rounded corners. 我正在创建一个简单的计时器应用程序,并希望将计时器圈子调整为带有圆角的视图大小。 The timer path is added within the background views class.
计时器路径已添加到背景视图类中。
On my iPhone XS Max, it looks like this: 在我的iPhone XS Max上,它看起来像这样:
But, on the iPhone SE simulator, it looks like this: 但是,在iPhone SE模拟器上,它看起来像这样:
How can I ensure the timer circles resize correctly? 如何确保计时器圈正确调整大小?
This is my code for the sizing of the circle paths: 这是我确定圆形路径大小的代码:
private func addTimerCircle() {
let translateAmount = -(CGFloat.pi/2)
let circlePath = UIBezierPath(arcCenter: CGPoint.zero, radius: (self.frame.width - 64)/2, startAngle: translateAmount, endAngle: (2*CGFloat.pi)+translateAmount, clockwise: true)
addTrackLayer(withPath: circlePath)
}
private func addTrackLayer(withPath circlePath: UIBezierPath) {
let trackLayer = CAShapeLayer()
trackLayer.path = circlePath.cgPath
trackLayer.strokeColor = UIColor.red.withAlphaComponent(0.5).cgColor
trackLayer.lineWidth = 10
trackLayer.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
trackLayer.fillColor = UIColor.clear.cgColor
layer.addSublayer(trackLayer)
}
Why does the circlePath
not resize based on frame.width
on the different screen sizes? 为什么
circlePath
不是基于调整frame.width
在不同的屏幕尺寸?
Thanks for any help! 谢谢你的帮助!
View controller views are loaded with the size they have in Interface Builder, and once they are added to the hierarchy the view is resized to the actual size. 视图控制器视图将以它们在Interface Builder中具有的大小加载,并且一旦将它们添加到层次结构中,视图就会被调整为实际大小。
I'm guessing you're calling addTimerCircle()
on viewDidLoad()
, which happens before the final sizing is applied. 我猜想您是在
viewDidLoad()
addTimerCircle()
上调用addTimerCircle()
viewDidLoad()
,这发生在应用最终大小调整之前。
You could override viewDidLayoutSubviews()
, which will be called when the view size changes, and add/update the shape there, but my suggestion would be to subclass UIView
, add the shape layer as a property, and update it's frame on layoutSubviews
. 您可以覆盖
viewDidLayoutSubviews()
,当视图大小更改时将调用它,并在那里添加/更新形状,但我的建议是将UIView
子类化,将形状图层添加为属性,并更新layoutSubviews
上的框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.