正如我之前做过的数百次一样,我正在用png图像创建自定义按钮。

但是由于某种原因,这次图像没有随按钮尺寸缩放。

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)

}


我得到的是一个蓝色框,其中按钮的图像小于按钮的框架。像是被填充的东西...

最佳答案

添加并尝试

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

关于ios - UIButton图像未调整大小-Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36930133/

10-12 05:27