iOS的Segment简单使用

创建
//初始化Segmented控件
- (void)initSegmentedControl
{
    NSArray *segmentedData = [[NSArray alloc]initWithObjects:@"订餐",@"商品",@"买单",nil];
    self.headerSegment = [[UISegmentedControl alloc]initWithItems:segmentedData];
    //segment宽
    CGFloat segmentW = 180;
    //segment高
    CGFloat segmentH = 30;
    //segmentX位置
    CGFloat segmentX = ([UIScreen mainScreen].bounds.size.width - segmentW) * 0.5;
    //segmentY位置
    CGFloat segmentY = 27;
    self.headerSegment.frame = CGRectMake(segmentX,segmentY,segmentW,segmentH);
    //这个是设置按下按钮时的颜色
    self.headerSegment.tintColor = WYRGBColor(255, 45, 80);
    //默认选中的按钮索引
    self.headerSegment.selectedSegmentIndex = 0;
    /*
     下面的代码实同正常状态和按下状态的属性控制,比如字体的大小和颜色等
     */
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName,WYRGBColor(255, 45, 80), NSForegroundColorAttributeName, nil];
    [self.headerSegment setTitleTextAttributes:attributes forState:UIControlStateNormal];
    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self.headerSegment setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
    //设置分段控件点击相应事件
    [_headerSegment addTarget:self action:@selector(segmentSelect:)forControlEvents:UIControlEventValueChanged];
    //添加到视图
    self.navigationItem.titleView = self.headerSegment;

    //从其他页面跳转
    if (self.segmentSelectNumber >= 1) {
        self.headerSegment.selectedSegmentIndex = self.segmentSelectNumber;
        [self segmentSelect:self.headerSegment];
    }else{
        //默认选择第一个控制器
        self.headerSegment.selectedSegmentIndex = 0;
        [self segmentSelect:self.headerSegment];
    }
}
点击segment按钮
//segment点击,控制器创建
-(void)segmentSelect:(UISegmentedControl*)seg{
    
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            if (self.orderFoodeVc == nil) {
                self.orderFoodeVc = [[RBOrderFoodViewController alloc]init];
                [self addChildViewController:self.orderFoodeVc];
                [self.view addSubview:self.orderFoodeVc.view];
            }
            self.orderGoodVc.view.hidden = YES;
            self.orderPayVc.view.hidden = YES;
            self.orderFoodeVc.view.hidden = NO;
        }
            break;
        case 1:
        {
            if (self.orderGoodVc == nil) {
                self.orderGoodVc = [[RBOrderGoodsViewController alloc]init];
                [self addChildViewController:self.orderGoodVc];
                [self.view addSubview:self.orderGoodVc.view];
            }
            self.orderFoodeVc.view.hidden = YES;
            self.orderPayVc.view.hidden = YES;
            self.orderGoodVc.view.hidden = NO;
        }
            break;
        case 2:
        {
            if (self.orderPayVc == nil) {
                self.orderPayVc = [[RBOrderFoodPayViewController alloc]init];
                [self addChildViewController:self.orderPayVc];
                [self.view addSubview:self.orderPayVc.view];
            }
            self.orderFoodeVc.view.hidden = YES;
            self.orderGoodVc.view.hidden = YES;
            self.orderPayVc.view.hidden = NO;
        }
            break;
            
        default:
            break;
    }
}

你可能感兴趣的:(iOS的Segment简单使用)