Example of a simple menu created by iOS

Source: Internet
Author: User

-(void) Createmeunview
{
UIButton *showmeunviewbutton = [[UIButton alloc]initwithframe:cgrectmake (0, 0, 100, 40)];
Showmeunviewbutton.center = Self.view.center;
[Showmeunviewbutton settitle:@ "Display menu" forstate:uicontrolstatenormal];
[Showmeunviewbutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
[Showmeunviewbutton Settitlecolor:[uicolor Lightgraycolor] forstate:uicontrolstateselected];
[Showmeunviewbutton addtarget:self Action: @selector (showmeunviewbuttonaction:) forControlEvents: UIControlEventTouchUpInside];
[Self.view Addsubview:showmeunviewbutton];

Showmeunview = [Self Createmeunview:showmeunviewbutton buttontitles:@[@ "1", @ "2", @ "3", @ "4", @ "5"] Menubuttontagstart : 300];
[Self.view Addsubview:showmeunview];
}

-(void) Showmeunviewbuttonaction: (UIButton *) button
{
button.selected =!button.selected;
if (button.selected) {
Showmeunview.hidden = NO;
}else
{
Showmeunview.hidden = YES;
}
}

#pragma mark--encapsulates the method of creating menus
-(UIView *) Createmeunview: (UIButton *) button buttontitles: (Nsarray *) buttontitles Menubuttontagstart: (NSInteger) Tagstart
{
The button menu appears below this parameter, the Buttontitles menu button's caption array, the Tagstart menu button tag Start value is used to distinguish which button is clicked
UIView *menuview = [[UIView alloc]initwithframe:cgrectmake (Button.minx, Button.maxy, Button.width, 41 * Buttontitles.count)];
Menuview.hidden = YES;
Menuview.backgroundcolor = [Uicolor blackcolor];

for (int i = 0; i < Buttontitles.count; i++)
{
UIButton *menubutton = [[UIButton alloc]initwithframe:cgrectmake (0, * I, Menuview.width, 40)];
Menubutton.backgroundcolor = [Uicolor Browncolor];
[Menubutton Settitle:buttontitles[i] forstate:uicontrolstatenormal];
MenuButton.titleLabel.font = font (14);
[Menubutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
[Menubutton addtarget:self Action: @selector (menubuttonaction:) forcontrolevents:uicontroleventtouchupinside];
Menubutton.tag = Tagstart + i;
[Menuview Addsubview:menubutton];
}

return menuview;
}

-(void) Menubuttonaction: (UIButton *) button
{
NSLog (@ "%lu", Button.tag);
}

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.