效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self layoutUI]; 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 - (void)layoutUI { 21 UISegmentedControl *smtCContentOffset = [[UISegmentedControl alloc] initWithItems:@[@"First", @"Second", @"Third"]]; 22 smtCContentOffset.tintColor = [UIColor colorWithRed:1.000 green:0.434 blue:0.469 alpha:1.000]; 23 smtCContentOffset.selectedSegmentIndex = 2; 24 smtCContentOffset.momentary = YES; 25 smtCContentOffset.center = self.view.center; 26 27 NSLog(@"分段卡的选项个数=%lu", (unsigned long)[smtCContentOffset numberOfSegments]); //分段卡的选项个数=3 28 29 //设置某个选项不可用 30 [smtCContentOffset setEnabled:NO forSegmentAtIndex:0]; 31 NSLog(@"分段卡的第一个选项是否可用?%@", [smtCContentOffset isEnabledForSegmentAtIndex:0] ? @"是" : @"否"); //分段卡的第一个选项是否可用?否 32 [self.view addSubview:smtCContentOffset]; 33 } 34 35 @end
时间: 2024-10-12 14:57:41