========================================================== =============================== 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