Swift创建一个图文共存的按钮


import UIKit

class BackButton: UIButton

{

override func titleRectForContentRect(contentRect: CGRect) -> CGRect 

return CGRectMake(self.frame.size.width/3.0, self.frame.size.height/6.0, self.frame.size.width/4.0*3.0, self.frame.size.height/3.0*2.0) 

}

override func imageRectForContentRect(contentRect: CGRect) -> CGRect 

return CGRectMake(0, self.frame.size.height/6.0, self.frame.size.width/4.0, self.frame.size.height/3.0*2.0) 

}

}



现在可以引用这个按钮了

现在你可以使用这个按钮了

let backBtn = BackButton()

backBtn.frame = CGRectMake(0, 0, 60, 30)

backBtn.setImage(UIImage(named: "backImg"), forState:UIControlState.Normal)

backBtn.setTitle("返回", forState: UIControlState.Normal)

你可能感兴趣的:(Swift创建一个图文共存的按钮)