简体   繁体   中英

iOS - UIButton image doesn't fit as expected

I have a UIButton that has a default image and if a property is set it changes to the property object (an image);

With the default image, everything works properly and the image fits perfectly but when I'm using the image, I don't know why the image doesn't fit.

Here's the code:

    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
    }()

I'm expecting something like this 预期

But I'm getting this instead 输出

将此行添加到您的button配置

button.imageView?.contentMode = .scaleAspectFit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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