IOS custom View -- swtichbutton Switch

Source: Internet
Author: User
========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================

There is no toolbar in iOS, which is different from Android and does not adapt. Now we can implement the switch by ourselves.

First, add two images to your resource to indicate the ON and OFF statuses. Name them switch_off.png,switch_on.png respectively.Code, I believe you can understand ., If you have any questions, please leave a message.

Uicustomswitch. h

 
# Import <uikit/uikit. h> @ interface uicustomswitch: uibutton @ end

Uicustomswitch. m

# Import "uicustomswitch. H "@ implementation uicustomswitch-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {[self setbackgroundimage];} return self ;} -(void) handle {[self defined: [uiimage imagenamed: @ "switch_off.png"] forstate: uicontrolstatenormal]; [self defined: [uiimage imagenamed: @ "switch_on.png"] forstate: uicontrolstateselected] ; [Self addtarget: Self action: @ selector (touchuiinside :) forcontrolevents: uicontroleventtouchupinside]; self. selected = no;}-(ID) initwithcoder :( nscoder *) adecoder {self = [Super initwithcoder: adecoder]; If (Self) {[self setbackgroundimage];} return self ;} -(ibaction) touchuiinside :( ID) sender {uicustomswitch * Sw = (uicustomswitch *) sender; Sw. selected =! Sw. Selected;} @ end

Over

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.