1.UISegmentedControl字體
UISegmentedControl *segC =[[UISegmentedControl alloc]initWithItems:@[@"傳感器",@"牀墊"]; segC.layer.cornerRadius=5; segC.tintColor = btnBackgColor; //字體大小 UIFont *font = [UIFont boldSystemFontOfSize:14]; NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; [segC setTitleTextAttributes:attributes forState:UIControlStateNormal]; //字體顏色 segC.layer.borderWidth = 2; segC.layer.borderColor = [btnBackgColor CGColor]; //默認選擇 [segC setSelectedSegmentIndex:0]; [segC addTarget:self action:@selector(btnChanged:) forControlEvents:UIControlEventValueChanged]; //添加到界面,並肯定大小位置 [self.view addSubview:segC]; [segC mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(segC.superview).with.offset(statusNavigationBarH+marginL); make.left.equalTo(segC.superview).with.offset(marginL); make.centerX.equalTo(segC.superview); make.height.mas_equalTo(40); }]; -(void)btnChanged:(UISegmentedControl *)segc{ if (segc.selectedSegmentIndex == 0) { Log(@"傳感器"); } else { Log(@"牀墊"); } }