Custom segmentcontrol using buttons in IOS

Source: Internet
Author: User

// Author: smilelance

// From: http://blog.csdn.net/smilelance

# Import <uikit/uikit. h>

@ Interface pdesegmentcontrol:
Uiview

{

Nsmutablearray * segmentbuttons;

Nsmutablearray * buttonimgnames;

}

@ Property (readonly,
Nonatomic) nsinteger selectedsegmentindex;

-(ID) initwithframe :( cgrect) frame items :( nsarray *) itemarray;

-(Void) setsegmentindex :( nsinteger) index;

// Segment Control

// Nsarray * segmentitems = [nsarray arraywithobjects: @ "not approved", @ "approved", nil];

// Uisegmentedcontrol * segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems: segmentitems];

// Segmentedcontrol. Frame = cgrectmake (310,295,160, 36 );

// [Segmentedcontrol addtarget: Self action: @ selector (segmentaction :) forcontrolevents: uicontroleventvaluechanged];

// Segmentedcontrol. segmentedcontrolstyle = uisegmentedcontrolstylebordered;

// Segmentedcontrol. selectedsegmentindex = 0;

// Myborrowrequesttype = segmentedcontrol. selectedsegmentindex;

// [Self. View addsubview: segmentedcontrol];

@ End

# Import "pdesegmentcontrol. H"

# Define segment_unselected 0

# Define segment_selected 1

@ Implementation pdesegmentcontrol

-(ID) initwithframe :( cgrect) Frame

{

Self = [Super
Initwithframe: frame];

If (Self ){

// Initialization code

}

Return self;

}

-(ID) initwithframe :( cgrect) frame items :( nsarray *) itemarray

{

Self = [Super
Initwithframe: frame];

If (Self ){

Int segmentcount = [itemarray
Count];

Segmentbuttons = [nsmutablearray
Alloc] init];

Buttonimgnames = [nsmutablearray
Alloc] init];

Float segmentwidth = frame. Size. width/segmentcount;

For (INT I = 0; I <segmentcount; I ++ ){

Uibutton * button = [uibutton
Buttonwithtype: uibuttontypecustom];

Button. Frame =
Cgrectmake (segmentwidth * I, 0,

Segmentwidth, frame. Size. Height );

If (I = 0 ){
// Left

[Buttonimgnames
Addobject: @ "seg_btn_left_nor.png"];

[Buttonimgnames
Addobject: @ "seg_btn_left_sel.png"];

} Else if (I = segmentCount-1 ){
// Right

[Buttonimgnames
Addobject: @ "seg_btn_right_nor.png"];

[Buttonimgnames
Addobject: @ "seg_btn_right_sel.png"];

} Else {// middle

}

Button. Tag = I;

[Button addtarget: Self
Action: @ selector (segmentaction :)
Forcontrolevents: uicontroleventtouchupinside];

[Button settitle: [itemarray
Objectatindex: I] forstate: uicontrolstatenormal];

[Segmentbuttons
Addobject: button];

[Self addsubview: button];

}

[Self
Setsegmentindex: 0];

}

Return self;

}

-(Void) setsegmentindex :( nsinteger) Index

{

_ Selectedsegmentindex = index;

[Self segmentaction: [segmentbuttons
Objectatindex: Index];

}

-(Void) segmentaction :( ID) sender

{

Uibutton * button = (uibutton *) sender;

Int tag = button. Tag;

For (INT I = 0; I <[segmentbuttons
Count]; I ++ ){

Int nameoffset = segment_unselected;

If (TAG = I ){

Nameoffset = segment_selected;

}

Uibutton * segbutton = [segmentbuttons
Objectatindex: I];

[Segbutton setbackgroundimage: [uiimage
Imagenamed: [buttonimgnames
Objectatindex: I * 2 + nameoffset]

Forstate: uicontrolstatenormal];

}

}

/*

// Only override drawrect: If you perform custom drawing.

// An empty implementation adversely affects performance during animation.

-(Void) drawrect :( cgrect) rect

{

// Drawing code

}

*/

@ End

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.