分段控件UISegmentedControl
分段控件UISegmentedControl继承与UIControl
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"全部商家", @"优惠商家", @"我的"]]; segmentedControl.center = CGPointMake(182, 45); // 每个segment的大小默认平分整个segmentedControl的大小,如果把apportionsSegmentWidthsByContent设置为YES,会根据内容来分配每一个segment的大小。 segmentedControl.apportionsSegmentWidthsByContent = YES; // 设置当前选中segment下标 segmentedControl.selectedSegmentIndex = 1; // 选中瞬间有效果,之后没有,恢复(不怎么用) // segmentedControl.momentary = YES; // 字体颜色 segmentedControl.tintColor = [UIColor orangeColor]; // segmentedControl.frame = CGRectMake(10, 30, 345, 30); // 给segmentedControl添加事件 [segmentedControl addTarget:self action:@selector(doTapSegmentedControl:) forControlEvents:(UIControlEventValueChanged)]; [self.view addSubview:segmentedControl]; [segmentedControl release];
- (void)doTapSegmentedControl:(UISegmentedControl *)segmentControl { NSLog(@"O(∩_∩)O哈哈~"); switch (segmentcontrol.selectedSegmentIndex) { case 0: self.view.backgroundColor = [UIColor redColor]; break; case 1: self.view.backgroundColor = [UIColor yellowColor]; break; case 2: self.view.backgroundColor = [UIColor blueColor]; break; default: break; } }