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

Switch Between 2 UIViewControllers in a NavigationBar Underneath

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

我已经在 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;
}

有人知道问题出在哪里吗?谢谢

我找到了解决办法。 tableview 在导航栏下面的原因是由于导航栏的半透明。当我设置这个时:

self.navigationController.navigationBar.translucent = NO;

视图保留在导航栏下方。 UITableView 上方仍然有一个黑条我通过使用 UIViewController 而不是使用 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;
    }
}

}