ios - 如何调整所有屏幕尺寸的 View ?

标签 ios swift uiview autolayout

我正在创建一个简单的计时器应用程序,并希望将计时器圆圈的大小调整为圆角 View 的大小。计时器路径添加在背景 View 类中。

在我的 iPhone XS Max 上,它看起来像这样:

Timer on iPhone XS Max

但是,在 iPhone SE 模拟器上,它看起来像这样:

Timer on iPhone SE

如何确保计时器圈正确调整大小?

这是我用于确定圆形路径大小的代码:

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

为什么 circlePath 在不同的屏幕尺寸上不会根据 frame.width 调整大小?

感谢您的帮助!

最佳答案

View Controller View 以它们在 Interface Builder 中的大小加载,一旦它们被添加到层次结构中, View 就会调整为实际大小。

我猜您是在 viewDidLoad() 上调用 addTimerCircle(),这发生在应用最终大小调整之前。

您可以覆盖 viewDidLayoutSubviews(),它会在 View 大小更改时调用,并在那里添加/更新形状,但我的建议是子类化 UIView ,将形状图层添加为属性,并在 layoutSubviews 上更新它的框架。

关于ios - 如何调整所有屏幕尺寸的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55641546/

相关文章:

iphone - 核心数据 : Dealing with polymorphic associations?

ios - 如何拍摄未呈现的 UIView 的快照?

swift - 更新属性包装器(如 @StateObject)会影响不使用该属性的其他 View 渲染

swift - 如何在 swift 中将井字棋板绘制为 UIView?

ios - UIView animateWithDuration 使应用程序在从 dispatch_async block 调用时挂起

iOS检测蓝牙连接/断开

ios - 如何捕获 func LocationManager 的 GPS 数据?

javascript - Fire Event before 'focus' kicks in/Fire Event before keyboard appears on iOS

ios - NEHotspotConfigurationManager 没有密码/SSID 无效的错误

swift - 如何使用 Storyboard从弹出窗口快速访问父 View Controller