![](/img/trans.png)
[英]UIButton imageView - after UIViewContentModeScaleAspectFill how to move the image up?
[英]UIButton with the imageView set to UIViewContentModeScaleAspectFill doesn't fill completely with small images
我的應用程序中有一個UIButton
,而不是文本我給它一個圖像,因為它作為一個縮略圖按鈕服務器,將在點擊時顯示完整的圖像。
我看到這個答案似乎是可接受的實現,但是圖像小於按鈕本身,圖像視圖實際上並不像contentMode那樣填充按鈕,或者像圖像視圖一樣,如下所示:
如您所見,使用圖像視圖可以正確擴展,但使用按鈕則不會。 (按鈕背景設置為藍色,以便您可以看到它未顯示的位置。)
我正在做的代碼很簡單:
myButton.setImage(UIImage(named: "pokemon"), forState: .Normal)
myButton.imageView!.contentMode = .ScaleAspectFill
如何更改它以使其正確填充?
示例項目: http : //cl.ly/0z2T1A1Z3v3m
將contentHorizontalAlignment
和contentVerticalAlignment
設置為.Fill
UIButton
使其完全擴展。
將圖像設置為按鈕的背景。 這將填充按鈕的框架,無需更改內容模式。 使用setBackgroundImage()
方法將圖像設置為按鈕的背景。 祝好運!
編輯
選項1 :
如何在圖像視圖上使用UIImageView
和UITapGestureRecognizer
以及所需的contentMode
?
選項2 :
將圖像裁剪為按鈕的大小,並將其設置為按鈕的背景圖像。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.