【问题标题】:UITableViewController inside UITabBarController inside UINavigationControllerUINavigationController 内部的 UITabBarController 内部的 UITableViewController
【发布时间】:2013-12-26 13:52:36
【问题描述】:

我的问题

我在 UINavigationController 中使用 UITabBarController。 UITabBarController 内部有三个 tableview。如图所示,第一个表格视图正确显示,而其他两个表格视图部分隐藏在导航栏后面。我该如何解决这个问题?

我的层次结构:

  • 根目录:UINavigationController
    • UITabBar 控制器
      • UITableViewController (table1,table2,table3)

这是我的代码:

AppDelegate.m

#import "AppDelegate.h"
#import "TableViewController.h"
@interface AppDelegate()
@property UINavigationController* nav;
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    TableViewController* table1 = [[TableViewController alloc]init];
    TableViewController* table2 = [[TableViewController alloc]init];
    TableViewController* table3 = [[TableViewController alloc]init];
    table1.title = @"table1";
    table2.title = @"table2";
    table3.title = @"table3";
    UITabBarController* t = [[UITabBarController alloc] init];
    [t setViewControllers:@[table1,table2,table3]];
    self.nav = [[UINavigationController alloc] initWithRootViewController:t];
    [self.window setRootViewController:self.nav];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application{}
- (void)applicationDidEnterBackground:(UIApplication *)application{}
- (void)applicationWillEnterForeground:(UIApplication *)application{}
- (void)applicationDidBecomeActive:(UIApplication *)application{}
- (void)applicationWillTerminate:(UIApplication *)application{}
@end

TableViewController.m

#import "TableViewController.h"
@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:style];
    if (self) {}
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self.tabBarController.view layoutSubviews];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* c = [[UITableViewCell alloc] init];
    [c.textLabel setText:[NSString stringWithFormat:@"%d", indexPath.row]];
    return c;
}
@end

【问题讨论】:

  • 您是否尝试设置 self.tableView.contentInset = UIEdgeInsetsMake(64.0f, 0.0, 0.0, 0.0);?在你的 UITableviewControllers 中
  • self.tableView.contentInset = UIEdgeInsetsMake(64.0f, 0.0, 0.0, 0.0);将使 table2 和 table3 看起来不错,但也会使 table1 降低 64 点。
  • @১২৩:谢谢它有效。顺便说一句,你的名字是什么语言?
  • 我迟到了,但是......遇到同样问题的任何人都应该取消选中自动调整滚动视图插图......从属性检查器中可以解决问题

标签: ios objective-c uitableview uinavigationcontroller uitabbarcontroller


【解决方案1】:

通常,层次结构是

UITabBarController
    - UINavigationController
        - UITableViewController

你为什么要把导航控制器放在最上面?尝试改用充满导航控制器的标签栏重新组织。

【讨论】:

  • 我正在尝试这样做,因为我希望当 table1 中的一行被选中时,它将在保持导航栏的同时全屏推送一个新的视图控制器。我不希望新推送的新视图中的标签栏。我也知道想要简单地将标签栏设置为消失,因为那样会不流畅。
  • Tab bar 控制器并非设计为位于视图层次结构顶部的任何位置。因此,即使您找到了解决此问题的技巧(因为它可能与控制器层次结构无关),随着您的开发进展,您肯定会遇到其他人。我会把标签栏的隐藏作为你的焦点。
  • 尽管我不想这么说,但 UITabBarController 的文档说 UITabBarController 必须位于层次结构的根部。 “在部署标签栏界面时,您必须将此视图安装为窗口的根。与其他视图控制器不同,标签栏界面不应安装为另一个视图控制器的子级。”
  • @user94602 现在您可以使用它来全屏显示新的视图控制器:newViewController.hidesBottomBarWhenPushed = YES;,这适用于 coneybeare 引用的层次结构。
【解决方案2】:

我昨天遇到了同样的问题,并决定解决它。这只是一个妨碍的类,所以这里是重写:

DRTabBarController.h

//
// Created by Dan Rosenstark on 2/28/15.
// Copyright (c) 2015 Confusion Studios LLC. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DRTabBarController : UIViewController <UITabBarDelegate>;

@property (nonatomic, strong) NSArray *viewControllers;
@property (nonatomic, strong) UITabBar *tabBar;
@property (nonatomic, strong) UIView *mainView;

@end

DRTabBarController.m

//
// Created by dr2050 on 2/28/15.
// Copyright (c) 2015 Confusion Studios LLC. All rights reserved.
//

#import "DRTabBarController.h"


@implementation DRTabBarController {


}


- (instancetype)init {
    self = [super init];
    if (self) {
        self.tabBar = [[UITabBar alloc] init];
        self.tabBar.delegate = self;
        self.tabBar.tintColor = [UIColor whiteColor];
        self.tabBar.barStyle = UIBarStyleBlack;
        self.tabBar.backgroundColor = [UIColor blackColor];

        self.mainView = [[UIView alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [self.view addSubview:self.tabBar];
    [self.view addSubview:self.mainView];
}

- (void)setViewControllers:(NSArray *)viewControllers {
    _viewControllers = viewControllers;

    NSMutableArray *tabBarItems = [NSMutableArray array];
    for (UIViewController *controller in viewControllers) {
        UITabBarItem *item = controller.tabBarItem;
        [tabBarItems addObject:item];
    }
    self.tabBar.items = tabBarItems;
}


- (void)viewWillAppear:(BOOL)animated {
    [self.tabBar setSelectedItem:self.tabBar.items.firstObject];
    [self tabBar:self.tabBar didSelectItem:self.tabBar.items.firstObject];
}

- (void)viewDidAppear:(BOOL)animated {



}

-(void)viewDidLayoutSubviews {
    CGRect frame = self.view.bounds;
    UITabBarController *throwaway = [[UITabBarController alloc] init];
    frame.size.height = throwaway.tabBar.frame.size.height;
    frame.origin.y = self.view.bounds.size.height - frame.size.height;
    self.tabBar.frame = frame;
    self.tabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

    frame = self.view.bounds;
    frame.size.height -= self.tabBar.frame.size.height;

    float navbarHeight = self.navigationController.navigationBar.frame.size.height;
    // cannot use UIApplication.sharedApplication.statusBarFrame.size.height because
    // reports are not right with in-call status bar
    float statusBarHeight = UIApplication.sharedApplication.statusBarHidden ? 0 : 20;
    float topBarHeight = navbarHeight + statusBarHeight;

    frame.origin.y += topBarHeight;
    frame.size.height -= topBarHeight;
    self.mainView.frame = frame;
    self.mainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    int index = [self.tabBar.items indexOfObject:item];
    NSArray *subviews = self.mainView.subviews;
    for (UIView *view in subviews) {
        [view removeFromSuperview];
    }
    UIView *view = [[self.viewControllers objectAtIndex:index] view];
    view.frame = self.mainView.bounds;
    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.mainView addSubview:view];
}


@end

注意:里面有一个神奇的变量——20——用于通话状态栏,它与周围的导航有着完全不同的关系......

对此的任何帮助将不胜感激,但确实有效。

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 2010-10-16
    • 2019-03-03
    • 2010-11-24
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多