Some public methods in Swift's UIButton class can be overridden, so if you want to write your own UIButton, simply inherit the UIButton class and rewrite the appropriate method.
The system's UIButton can add the picture, may also add the title, but cannot simultaneously all add, how to do?
Import Uikitclass 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) }}
The above simple class changes the position of the picture and the caption in UIButton, and the button created with the class can add both the picture and the text to the button as follows:
Let backbtn = Backbutton () backbtn.frame = CGRectMake (0, 0,.) backbtn.setimage (UIImage (named: "Backimg"), Forstate: Uicontrolstate.normal) Backbtn.settitle ("Back", ForState:UIControlState.Normal)
More ways to rewrite, to be continued ...
Swift customizes its own button style