gpt4 book ai didi

swift - 将 translatesAutoResizingMaskIntoConstraints 设置为 false 会导致 UITextFields 右 View 出现模糊的布局问题

转载 作者:行者123 更新时间:2023-11-28 07:39:57 25 4
gpt4 key购买 nike

我不知道是否让每个对象都将 translatesAutoresizingMaskIntoConstraints 标记为 false 是个坏习惯,但这是我与此相关的问题;我有 UITextField 对象,我将其 rightView 设置为 UIImageView,但是当 UIImageView 的 translatesAutoresizingMaskIntoConstraints 属性在调试 View 层次结构中设置为 false 时,出现模糊布局警告,苹果的文档在 translatesAutoresizingMaskIntoConstraints 主题下说:

If you want to use Auto Layout to dynamically calculate the size and >position of your view, you must set this property to false, and then >provide a non ambiguous, nonconflicting set of constraints for the view.

但是有UITextField的方法

open func rightViewRect(forBounds bounds: CGRect) -> CGRect

那个

Returns the drawing location of the receiver’s right overlay view.

此外,当我尝试在 rightView 和 UITextField 之间施加约束时,它说它们不在同一层次结构下。那么,为什么将该标志设置为 false 会导致布局不明确?

let imageView = UIImageView(image: UIImage(named: "infoIcon"))
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false //this line causes ambiguous layout
imageView.widthAnchor.constraint(equalToConstant: 15).isActive = true
textField.rightViewMode = .unlessEditing
textField.rightView = imageView

最佳答案

我遇到了类似的问题,但就我而言,我忘记了将 add translatesAutoresizingMaskIntoConstraints 设置为 false。

您写的关于 translatesAutoresizingMaskIntoConstraints 的内容是正确的。通常,当您使用设置框架或边界时,这不是很好的结合。

无论如何,我试过你的代码,我知道可能会有什么影响;

  • 首先检查您的 UITextField 约束。如果您的约束条件不合适,那么它可能会在添加 rightView 后中断。
  • 其次使用如下所示的框架初始化您的 imageView;
let imageView = UIImageView(frame: .zero)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "logo") imageView.contentMode = .scaleAspectFit
imageView.widthAnchor.constraint(equalToConstant: 15).isActive = true
textField.rightViewMode = .always
textField.rightView = imageView
  • 最后我认为如果你不使用 imageView.widthAnchor.constraint(equalToConstant: 15).isActive = true 会更好为您的文本字段使用具有最佳分辨率的图像,其余的就可以了。

关于swift - 将 translatesAutoResizingMaskIntoConstraints 设置为 false 会导致 UITextFields 右 View 出现模糊的布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52551020/

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