gpt4 book ai didi

ios - Swift:自动布局以编程方式导致错误的帧大小(小数)

转载 作者:行者123 更新时间:2023-11-28 13:22:54 48 4
gpt4 key购买 nike

我正在尝试在容器 View 中设置 9:16 纵横比 View 。以下代码在 viewDidLayoutSubviews 中设置约束,以便在正确的位置考虑自动布局。它还调用 layoutIfNeeded

虽然 maxWidth 的计算是正确的(在 iPhone X 上:609.7777777777778),但设置的约束会产生错误的帧大小(在 iPhone X 上:609.6666666666666)。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var orangeContainerView: UIView!
var inset: CGFloat = 16

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

setupAutolayout()
}

func setupAutolayout() {
let aspectRatioView = UIView()
aspectRatioView.backgroundColor = .blue
orangeContainerView.addSubview(aspectRatioView)
aspectRatioView.translatesAutoresizingMaskIntoConstraints = false

// Set up a 9:16 view
// calculate max width, and based on that the max height and see if it fits into the container
let maxWidth = orangeContainerView.frame.width - (2 * inset)
let targetHeight = maxWidth / 9 * 16
if targetHeight <= orangeContainerView.frame.height {
print("FRAME SIZE SHOULD BE: \(maxWidth) x \(targetHeight)")
aspectRatioView.topAnchor.constraint(equalTo: orangeContainerView.topAnchor).isActive = true
aspectRatioView.centerXAnchor.constraint(equalTo: orangeContainerView.centerXAnchor).isActive = true
aspectRatioView.widthAnchor.constraint(equalToConstant: maxWidth).isActive = true
aspectRatioView.heightAnchor.constraint(equalToConstant: targetHeight).isActive = true
}

orangeContainerView.layoutIfNeeded()
print(aspectRatioView.frame)
}

}

在 iPhone X 模拟器中打印如下:

FRAME SIZE SHOULD BE: 343.0 x 609.7777777777778
(16.0, 0.0, 343.0, 609.6666666666666)

IB 截图:IB screenshot

最佳答案

帧坐标不能是任意的。

如果设备比例为 2(例如 iPhone 8),框架边界将四舍五入为 1/2 磅。

如果设备比例为 3(例如 iPhone X),框架边界将四舍五入为 1/3 点。

因此,当您的浮点计算结果为 609.7777777777778 时,将该值设置为宽度约束将为您提供实际的帧宽度:

  • 在 iPhone X 上:609.6666666666666(609 和 2/3)
  • 在 iPhone 8 上:610.0

关于ios - Swift:自动布局以编程方式导致错误的帧大小(小数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58842306/

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