iOS 分度那控制UISegmentedControl添加到导航栏上的使用方法

//

//  RootViewController.m

//  Segement

//

//  Created by ancun on 14/9/4.

//  Copyright (c) 2015 ancun. All rights reserved.

//


#import "RootViewController.h"


@interface RootViewController ()

{

    

   UIView *_aview;

}

@end


@implementation RootViewController


-(void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    self.navigationController.navigationBarHidden =NO;

    self.navigationController.navigationBar.translucent = NO;

}



- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColorgrayColor];

    UISegmentedControl *segment = [[UISegmentedControlalloc]initWithItems:@[@"",@"",@"",@"绿"]];

    segment.frame =CGRectMake(60,100,200, 40);

    self.navigationItem.titleView = segment;

//    [self.view addSubview:segment];

    //[segment setTitle:@"nv" forSegmentAtIndex:2];//修改


    [segment insertSegmentWithTitle:@"hei"atIndex:4animated:YES];//添加一个分段

    segment.selectedSegmentIndex =1;//选择按下标

    segment.momentary =NO;//点完以后会起来,按钮(瞬间选中离开)默认为NO

    [segment setEnabled:NOforSegmentAtIndex:3];//不能使用的按钮(灰色)

    segment.tintColor=[UIColorredColor];

    //[dubai]分度添加响应方式

    [segment addTarget:selfaction:@selector(handelSegementControlAction:)forControlEvents:(UIControlEventValueChanged)];

    

    //[dubai]添加一个视图

    

    _aview = [[UIViewalloc]initWithFrame:CGRectMake(60,200,200, 200)];

    _aview.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:_aview];

    

    

}

//[dubai]点击分段控制执行相应的方法

- (void)handelSegementControlAction:(UISegmentedControl *)segment

{


    NSLog(@"%s",__FUNCTION__);

    switch (segment.selectedSegmentIndex) {

       case0:

        {

            

            

            _aview.backgroundColor = [UIColorredColor];

            

           break;

        }

       case1:

        {

            _aview.backgroundColor = [UIColor yellowColor];

           break;

        }

       case2:

        {

            

            _aview.backgroundColor = [UIColor blueColor];

           break;

            

        }

            

       case3:

        {

            _aview.backgroundColor = [UIColor greenColor];

           break;

        }

       default:

        {

            _aview.backgroundColor = [UIColor blackColor];

            

           break;

        }

           break;

    }

    


   



}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

iOS 分度那控制UISegmentedControl添加到导航栏上的使用方法_第1张图片

/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



你可能感兴趣的:(iOS)