簡體   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