在UINavigationController的导航栏中添加UISegmentedControl

来源:互联网 发布:大数据 股票预测模型 编辑:程序博客网 时间:2024/05/24 15:42

在导航栏中添加UISegmentedControl ,实现的效果:

image

实现的代码:

UISegmentedControl *segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 8.0f, 200.0f, 30.0f) ]; 
    [segmentedControl insertSegmentWithTitle:@"Food to eat" atIndex:0 animated:YES]; 
    [segmentedControl insertSegmentWithTitle:@"Food to avoid" atIndex:1 animated:YES]; 
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
    segmentedControl.momentary = YES; 
    segmentedControl.multipleTouchEnabled=NO; 
    [segmentedControl addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged]; 
    UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; 
    [segmentedControl release]; 
    self.navigationItem.rightBarButtonItem = segButton; 
    [segButton release];

原创粉丝点击