【iOS】UISegmentedControl


  • 初始

        UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:
                                       [NSArray arrayWithObjects: @"Mail", @”SMS”, nil]];
        segment.frame =  CGRectMake(10, 10, 200, 35);
        segment.selectedSegmentIndex = NSIntegerMax;
        segment.tag = 3;
        [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        segment.segmentedControlStyle = UISegmentedControlStyleBar;
        [self.view addSubview:segment];

  • 加入 badge,  要搭配 CustomBadge
    image

    - (IBAction) segmentAction:(id)sender
    {
        UISegmentedControl *segment = (UISegmentedControl*) sender;
        NSLog(@"UIControlEventValueChanged:%d", segment.selectedSegmentIndex);
        segment.enabled = NO;
    }
    - (void) viewDidLoad
    {

        UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:
                                       [NSArray arrayWithObjects: @"Mail", @"SMS", nil]];
        segment.frame =  CGRectMake(10, 10, 200, 35);
        segment.selectedSegmentIndex = NSIntegerMax;
        segment.tag = 3;
        [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        segment.segmentedControlStyle = UISegmentedControlStyleBar;
        [self.view addSubview:segment];
       
        CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:@"32"
                                                       withStringColor:[UIColor whiteColor]
                                                        withInsetColor:[UIColor greenColor]
                                                        withBadgeFrame:YES
                                                   withBadgeFrameColor:[UIColor whiteColor]
                                                             withScale:1.0
                                                           withShining:YES];       
       
        [customBadge1 setFrame:CGRectMake(90, 0, 20, 20)];
        [self.view addSubview:customBadge1];
       
        CustomBadge *customBadge2 = [CustomBadge customBadgeWithString:@"33"
                                                       withStringColor:[UIColor whiteColor]
                                                        withInsetColor:[UIColor redColor]
                                                        withBadgeFrame:YES
                                                   withBadgeFrameColor:[UIColor whiteColor]
                                                             withScale:1.0
                                                           withShining:YES];       
       
        [customBadge2 setFrame:CGRectMake(190, 0, 20, 20)];
        [self.view addSubview:customBadge2];

        [self.view bringSubviewToFront:customBadge1];
        [self.view bringSubviewToFront:customBadge2];
        }

 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com