How to programmatically add a UISegmentedControl to a container view

后端 未结 7 953
情话喂你
情话喂你 2020-12-12 17:54

How would I define the frame for a UISegmentedControl? I would like the segmented control to appear at the bottom of a container view i.e UI

7条回答
  •  清歌不尽
    2020-12-12 18:27

    U can do like this...

    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];
    
    [segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    segmentControl.frame = CGRectMake(10, 50, 300, 30);
    [segmentControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
    [segmentControl setSelectedSegmentIndex:0];
    [scrollView addSubview:segmentControl];
    [segmentControl release];
    

    Step 2:

    -(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
    {
    switch (segment.selectedSegmentIndex) {
        case 0:{
            //action for the first button (Current)
            break;}
        case 1:{
            //action for the first button (Current)
            break;}
        }
    }
    

提交回复
热议问题