2016-04-29 62 views
0

我正在創建一個帶有png圖像的自定義按鈕,因爲我之前已經完成了數百次。UIButton image not resizing - Swift

但是由於某種原因,這次圖像沒有按比例縮放。

func constructInfoViewBtns() { 
    let buttonsView = UIView.init(frame: CGRectMake(0, 90, self.infoView!.frame.width, 90)) 

    let playPauseBtn = UIButton(type: UIButtonType.Custom) as UIButton 
    let playPauseBtnImg : UIImage = UIImage(named: "pauseBtn")! 
    playPauseBtn.setImage(playPauseBtnImg, forState: .Normal) 
    playPauseBtn.imageView?.contentMode = UIViewContentMode.ScaleAspectFill 
    playPauseBtn.frame = CGRectMake(0, 0, 55, 55) 
    playPauseBtn.backgroundColor = UIColor.blueColor() 
    playPauseBtn.center = CGPointMake(buttonsView.frame.width/2, buttonsView.frame.height/2) 
    playPauseBtn.addTarget(self, action: "playPauseTrack:", forControlEvents: .TouchDown) 
    self.playPauseBtn = playPauseBtn 

    buttonsView.addSubview(self.playPauseBtn!) 

    self.infoView!.addSubview(buttonsView) 

} 

我得到的是一個藍色框,其中按鈕圖像小於按鈕的框架。喜歡它的填充或東西...

+0

嘗試設置的圖像作爲按鈕的背景圖像。 @broderickga –

+0

可能是圖片大小比按鈕框大 –

回答

1

添加此,並嘗試

playPauseBtn.imageEdgeInsets = UIEdgeInsetsMake(25,25,25,25) 
+0

這會有幫助嗎? – broderickga

+0

@broderickga - 你試過這個 –