简体   繁体   English

如何在所有屏幕尺寸上调整视图大小?

[英]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上,它看起来像这样:

iPhone XS Max上的计时器

But, on the iPhone SE simulator, it looks like this: 但是,在iPhone SE模拟器上,它看起来像这样:

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.

相关问题 自动调整视图对象的大小以适合不同的屏幕尺寸 - Auto resize view objects for different screen sizes 如何使用autolayout缩放所有屏幕尺寸的视图? - How to use autolayout to scale view for all screen sizes? 如何创建支持所有屏幕尺寸的视图? - How to create a view which supports all screen sizes? Obj-C,如何在所有iPhone尺寸上缩放子视图的高度? - Obj-C, how do I get the height of a sub view to scale, across all iPhone sizes? 如何在表格视图单元格中放置集合视图单元格(适用于所有屏幕尺寸?) - how to fit collection view cell in table view cell ( adaptive on all screen sizes?) 如何使用Xcode 8上的自动布局为不同屏幕尺寸调整对象大小? - How to resize objects for different screen sizes using auto layout on Xcode 8? 如何使圈子保持圈子并在不同的屏幕尺寸上调整大小? - How to make circles stay circles and resize on different screen sizes? 将所有图像调整为相等大小 - Resize all images to be of equal sizes 为每个屏幕尺寸自动调整大小/调整UIPickerView的大小 - autosize/resize UIPickerView for each screen sizes 调整UITableViewCell的大小以适合不同的屏幕尺寸 - Making a UITableViewCell resize to fit different screen sizes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM