Swift customizes its own button style

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.