繁体   English   中英

iOS-UIButton图像不符合预期

[英]iOS - UIButton image doesn't fit as expected

我有一个具有默认图像的UIButton ,如果设置了属性,它将更改为属性对象(图像);

使用默认图像时,一切正常运行,并且图像完美适合,但是当我使用图像时,我不知道为什么图像不适合。

这是代码:

    var user: User? {
        didSet {
            self.profileImageButton.setImage(self.user?.profileImage?.withRenderingMode(.alwaysOriginal) ?? #imageLiteral(resourceName: "plus_photo").withRenderingMode(.alwaysOriginal), for: .normal)
        }
    }

    lazy var profileImageButton: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "plus_photo").withRenderingMode(.alwaysOriginal), for: .normal)
        button.layer.cornerRadius = 80/2
        button.contentMode = .scaleAspectFill
        button.layer.masksToBounds = true
        button.clipsToBounds = true
        button.layer.borderWidth = 1
        button.layer.borderColor = .white
        button.addTarget(self, action: #selector(didTapPhoto), for: .touchUpInside)
        return button
    }()

我期待这样的事情 预期

但是我得到了这个 输出

将此行添加到您的button配置

button.imageView?.contentMode = .scaleAspectFit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM