一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

iOS创建简单的菜单的例子

时间:2016-04-17 编辑:简简单单 来源:一聚教程网

-(void)createMeunView
{
    UIButton *showMeunViewButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    showMeunViewButton.center = self.view.center;
    [showMeunViewButton setTitle:@"显示菜单" 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 -- 封装创建菜单的方法
-(UIView *)createMeunView:(UIButton *)button buttonTitles:(NSArray *)buttonTitles menuButtonTagStart:(NSInteger)tagStart
{
    //button菜单显示在这个参数下面,buttonTitles菜单按钮的标题数组,tagStart菜单按钮tag起始值用于区别点击了哪个按钮
    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, 41 * 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);
}

热门栏目