[英]UITableViewController inside UITabBarController inside UINavigationController
I am using a UITabBarController inside a UINavigationController. 我在UINavigationController中使用UITabBarController。 And there are three tableviews inside the UITabBarController.
UITabBarController中有三个tableviews。 As shown in the picture, the first table view shows correctly while the other two tableviews are partially hidden behind the navigation bar.
如图所示,第一个表视图正确显示,而其他两个tableviews部分隐藏在导航栏后面。 How can I fix this?
我怎样才能解决这个问题?
Here is my code: 这是我的代码:
#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
#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
Typically, the hierarchy is 通常,层次结构是
UITabBarController
- UINavigationController
- UITableViewController
Why are you trying to put the Navigation Controller on top? 你为什么要把导航控制器放在最上面? Try reorganizing using a tab bar full of navigation controllers instead.
尝试使用充满导航控制器的标签栏重新组织。
I hit this same problem yesterday, and decided to work around it. 我昨天遇到了同样的问题,并决定解决它。 It's just one class that gets in the way, so here's the rewrite:
它只是一个类阻碍了,所以这里是重写:
DRTabBarController.h 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 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
Note: There's one magic variable in there -- 20 -- for the in-call status bar, which has a totally different relationship to the surrounding nav... 注意:那里有一个魔术变量 - 20 - 用于呼叫状态栏,它与周围的导航器有完全不同的关系......
Any help with this would be appreciated, but does work. 任何有关这方面的帮助将不胜感激,但确实有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.