分段控件UISegmentedControl繼承與UIControlapp
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; } }