2017-05-29 68 views
0

我有一個UIButton有一個默認圖像,如果一個屬性設置它改變屬性對象(圖像);iOS - 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 
    }() 

我期待這樣的事情 expected

但我發現了這個代替 output

+0

即使我在clipToBounds和maskToBounds之間進行交換,它仍然保持不變,但是使用默認圖像(虛擬圖像),它正確適合 –

回答

1

此行添加到您的button配置

button.imageView?.contentMode = .scaleAspectFit 
+0

解決方法:D謝謝。我設置了button屬性而不是imageView。感謝您的幫助 –

+0

歡迎:) –