【问题标题】:UIImage not resizing according to UIImageView size width and heightUIImage 未根据 UIImageView 大小宽度和高度调整大小
【发布时间】:2021-12-28 17:34:43
【问题描述】:

我是一个 Swift 新手,我正在尝试使用 Storyboard 和 @IBInspectable 注释在 UITextField 的右侧添加一个 UIImageView。 但是,当我用特定大小定义 UIImageView 并添加 UIImage 时,UIImage 不会根据 UIImageView 大小调整大小。

这是我的代码:

    @IBInspectable var rightImage: UIImage? {
    didSet {
        guard let image = rightImage else {
            return
        }
        rightViewMode = .always
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        rightView = imageView

这是屏幕上的结果: Big big icon

如果您能告诉我我的代码有什么问题,请不要犹豫。 谢谢。

【问题讨论】:

  • 您需要使用自动布局,而不是框架,来配置图像视图。
  • 感谢马特,我在 UIImageView 中添加了约束,它起作用了!

标签: swift uiimageview storyboard uiimage ibinspectable


【解决方案1】:

我根据 Matt 在评论中的建议使用了约束,现在它正在工作:

let constraints = [
    imageView.heightAnchor.constraint(equalToConstant: 20),
    imageView.widthAnchor.constraint(equalToConstant: 20)
]

NSLayoutConstraint.activate(constraints)

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多