在下面的导航栏中切换2个视图控制器

时间:2015-01-30 14:08:07

标签: objective-c uiviewcontroller uinavigationbar uisegmentedcontrol childviewcontroller

我已经构建了一个UIViewController来包含另外两个UIViewControllers。我想在UINavigationBar中使用UISegment做的是在两个视图之间切换。当用户完成后,他们点击"完成"它会回到RootViewController。我得到了这个工作,除了UIViewControllers占用整个屏幕的大小,所以顶部部分被UINavigationBar覆盖。他们不适应UINavigationBar下的空间。我一直在尝试不同的技术而没有成功。

我已经在ChildUIViewControllers中尝试过但没有成功

self.edgesForExtendedLayout = UIRectEdgeNone;

这是我的代码:

RootViewController调用容器

ContainerController *controller = [[ContainerController alloc] init]
[self.navigationController pushViewController:controller animated:YES];

货柜代码

- (void)viewDidLoad {

[super viewDidLoad];
UIViewController *vc = [self getCurrentViewController]; // Returns Current ViewController to Display
[self addChildViewController:vc];
vc.view.frame = self.view.bounds;
[self.view addSubview:vc.view];

self.currentViewController = vc;
}

-(UIViewController *)getCurrentViewController{
UIViewController *vc;
switch ([[NSUserDefaults standardUserDefaults] integerForKey:@"currentView"]) {
    case kView1:{
        vc = (ViewController1 *)[[ViewController1 alloc] init];
    }break;
    case kView2:{
        vc = (ViewController2 *)[[ViewController2 alloc] init];
    }break;
}
return vc;
}

- (void)segmentedControlIndexChanged:(id)sender{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithInt:[sender selectedSegmentIndex]] forKey:@"currentView"];
[userDefaults synchronize];
UIViewController *vc = [self getCurrentViewController];

[self addChildViewController:vc];
[self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    [self.currentViewController.view removeFromSuperview];
    vc.view.frame = self.view.bounds;
    [self.view addSubview:vc.view];
} completion:^(BOOL finished) {
    [vc didMoveToParentViewController:self];
    [self.currentViewController removeFromParentViewController];
    self.currentViewController = vc;
}];
self.navigationItem.title = vc.title;
}

有人知道问题是什么吗?感谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 tableview位于导航栏下的原因是导航栏的半透明。当我设置它时:

self.navigationController.navigationBar.translucent = NO;

视图仍保留在导航栏下方。在UITableView上面还有一个黑条我修复了这个问题,而不是使用UITableViewController我用户UIViewController并添加了一个tableView。下面是一些代码,用于在容器uiviewcontroller中的两个视图之间进行循环:

- (void)viewDidLoad {
self.viewController1 = [[ViewController1 alloc] init];
self.viewController2= [[ViewController2 alloc] init];

switch ([[NSUserDefaults standardUserDefaults] integerForKey:@"dataView"]) {
case kDataView:{
    self.viewController1.view.hidden=YES;
    self.viewController2.view.hidden=NO;
}break;
case kChartView:{
    self.viewController1.view.hidden=NO;
    self.viewController2.view.hidden=YES;
}break;
}

[self.view addSubview:self.viewController1.view];
[self.view addSubview:self.viewController2.view];
}

 - (void)segmentedControlIndexChanged:(id)sender{

    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"dataView"]!=[sender selectedSegmentIndex]){
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSNumber numberWithInteger:[sender selectedSegmentIndex]] forKey:@"dataView"];
    [userDefaults synchronize];

    self.segmentedControl.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"dataView"];

    switch (self.segmentedControl.selectedSegmentIndex) {
        case kDataView:{
            self.viewController1.view.hidden=YES;
            self.viewController2.view.hidden=NO;
        }break;
        case kChartView:{
            self.viewController1.view.hidden=NO;
            self.viewController2.view.hidden=YES;
        }break;
    }
}

}