iOS菜鸟开发 设置分段导航器

一、设置分段导航器

在ViewController。m里写

- (void)viewDidLoad {

    [super viewDidLoad];

    [self creatView];  

}

// 设置页面

- (void)creatView{

    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithFrame:CGRectMake(5, 50, fDeviceWidth-10, 29)];

    [seg insertSegmentWithImage:@"按钮button" atIndex:0 animated:YES];

    [seg insertSegmentWithImage:@"标签label" atIndex:1 animated:YES];

    seg.tintColor = [UIColor redColor];

    seg.selectedSegmentIndex = 0;

    [seg addTarget:self action:@selector(seg:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:seg];

// 初始化button 和 label 页面的位置

    _button = [[ButtonView alloc] initWithFrame:CGRectMake(0, 160, fDeviceWidth, fDeviceHeight)];

    [self.view addSubview:_button];

    _label = [[LabelView alloc] initWithFrame:CGRectMake(fDeviceWidth, 160, fDeviceWidth, fDeviceHeight)];

    [self.view addSubview:_label];  

}

// 页面之间的切换

- (void)seg:(UISegmentedControl *)Seg{

    if (Seg.selectedSegmentIndex == 0) {

        Seg.selectedSegmentIndex = 0;

        [UIView animateWithDuration:0.2 animations:^{

            _button.hidden = NO;

            _button.frame = CGRectMake(0, 80, fDeviceWidth, fDeviceHeight - 30) ;

            _label.frame = CGRectMake(fDeviceWidth, 50, fDeviceWidth, fDeviceHeight);

        }];

    }else{

        Seg.selectedSegmentIndex = 1;

        [UIView animateWithDuration:0.2 animations:^{

            _button.frame = CGRectMake(-fDeviceWidth, 50, fDeviceWidth, fDeviceHeight);

            _label.frame = CGRectMake(0, 80, fDeviceWidth, fDeviceHeight - 30) ;

            _button.hidden = YES; 

        }];  

    }  

}

二、设置导航的2个页面 button页面 和 label 页面

①、

_oneButton = [[UIButton alloc] initWithFrame:CGRectMake( 20, 20, 20, 20)];

    _oneButton.backgroundColor = [UIColor brownColor];

    [self addSubview:_oneButton];

 _twoButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];

    _twoButton.backgroundColor = [UIColor greenColor];

    [self addSubview:_twoButton];

 ②、

    _oneLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 90, 200, 30)];

    _oneLabel.text = @"只要功夫深";

    _oneLabel.font = [UIFont systemFontOfSize:13];

    [self addSubview:_oneLabel];

 _twoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 200, 30)];

    _twoLabel.text = @"调查2222";

    _twoLabel.font = [UIFont systemFontOfSize:15];

    [self addSubview:_twoLabel];

posted @ 2016-04-13 18:09  悠扬007  阅读(245)  评论(0编辑  收藏  举报