当前位置: 代码迷 >> 综合 >> UISegmentedControl好多属性设置在iOS7.0之后都失去了失效---也就是“过期了”! iOS7.0之后我们可以用以下方法!
  详细解决方案

UISegmentedControl好多属性设置在iOS7.0之后都失去了失效---也就是“过期了”! iOS7.0之后我们可以用以下方法!

热度:47   发布时间:2023-12-22 18:52:04.0
NSArray *segmentedData = [[NSArray alloc]initWithObjects:@"申购",@"托管",nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedData];
    segmentedControl.frame = CGRectMake(0, 0,117.0 , 32.0);
    //圆角
    segmentedControl.layer.masksToBounds = YES;
    segmentedControl.layer.cornerRadius = 16.0;
    //边框
    segmentedControl.layer.borderWidth = 1.0;
    segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
    
    //这个是设置按下按钮时的颜色
    segmentedControl.tintColor = [UIColor whiteColor];
    segmentedControl.selectedSegmentIndex = 0;//默认选中的按钮 索引index;
    
    //实同正常状态和按下状态的属性控制,比如字体的大小和颜色等
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:15],NSFontAttributeName,[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
    
    //按住按钮时候字体颜色(高亮时候的颜色)
    NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor purpleColor] forKey:NSForegroundColorAttributeName];
    [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
    
    //设置分段控件点击响应事件
    [segmentedControl addTarget:self action:@selector(doSomethingInSegment:)forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;


//申购、托管segment按钮响应事件
-(void)doSomethingInSegment:(UISegmentedControl *)Seg
{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    
    switch (Index)
    {
        case 0:
           
            NSLog(@"申购");
//            [_tableView reloadData];
                        break;
        case 1:
            
            NSLog(@"托管");
//            [_tableView reloadData];
            break;
            
        default:
            break;
    }
}