Objective-C: UITabBarController with split views

I am new to Objective-C and I am trying to write my first iOS app. The idea is quite simple, but I didn’t work at the beginning of building the architecture.

I would like to create different views displayed on several tabs that should be dynamically created when the view loads. In addition, the application should be able to dynamically add tabs at run time. Table views should not go all over the screen, but should fill 2/3 of the top view. The remaining 1/3 at the bottom is again divided into two sub-items, which are not intended to be changed using the tablet switches.

What I did was create a UIWindow, a UITabBarController and two UIViewControllers (for two tabs) and one (or two, as shown), which should be at the bottom.

So far I have managed to switch between different types of tabs, but as soon as I try to resize the UIViewControllers for both tabs from CGMakeRect to any size, it always stays the same and covers the entire screen.

The view created below contains a button that somehow cannot be pressed. Maybe because it is closed from the tab views.

Can someone help me a little, how can I create these views?

Thanks a lot!

Here is my code:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *test = [[UIViewController alloc] init]; test.view.backgroundColor = [UIColor grayColor]; test.view.frame = CGRectMake(0, 0, 320, 200); UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button3 setTitle:@"View 3" forState:UIControlStateNormal]; button3.frame = CGRectMake(30.0, 30.0, 120.0, 50.0); [test.view addSubview:button3]; UITabBarController *tabBarController = [[UITabBarController alloc] init]; UIViewController *viewController1 = [[UIViewController alloc] init]; UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1]; [viewController1 setTabBarItem:tab1]; UIViewController *viewController2 = [[UIViewController alloc] init]; UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2]; [viewController2 setTabBarItem:tab2]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"View from Tab 1" forState:UIControlStateNormal]; button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); [viewController1.view addSubview:button]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button2 setTitle:@"View from Tab 2" forState:UIControlStateNormal]; button2.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); [viewController2.view addSubview:button2]; tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; self.window.rootViewController = tabBarController; [self.window addSubview:test.view]; [self.window makeKeyAndVisible]; 
+4
source share
1 answer

There is actually a way to achieve this thanks to custom container controllers:

Replace UITabBarController special subclass of UIViewController as the rootViewController of the window. Then in this viewDidLoad method (or elsewhere depending on your needs) you add almost your exact code from above with some minor changes. Here's the full code for the viewDidLoad method (I added comments on the modified lines):

 - (void)viewDidLoad { [super viewDidLoad]; UITabBarController *tabBarController = [[UITabBarController alloc] init]; UIViewController *viewController1 = [[UIViewController alloc] init]; //Mod1: Set an autoresizingMask //so that the view always fills the tabBarController view //if needed you can also set it frame here viewController1.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1]; [viewController1 setTabBarItem:tab1]; UIViewController *viewController2 = [[UIViewController alloc] init]; //Mod2: Same here viewController2.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2]; [viewController2 setTabBarItem:tab2]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"View from Tab 1" forState:UIControlStateNormal]; button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); [viewController1.view addSubview:button]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button2 setTitle:@"View from Tab 2" forState:UIControlStateNormal]; button2.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); [viewController2.view addSubview:button2]; tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; //Mod3: Set the frame and autoresizingMask of the tabBarController //to fill the rootVC view tabBarController.view.frame = self.view.bounds; tabBarController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; //Mod4: This is the important part: //add the tabBarController as childVC of the rootVC [self addChildViewController:tabBarController]; [self.view addSubview:tabBarController.view]; [tabBarController didMoveToParentViewController:self]; //Mod5: calculate the frame for the 'static' vc on the bottom float heightForStaticVC = 200.0f; float yPosForStaticVC = tabBarController.view.frame.size.height - tabBarController.tabBar.frame.size.height - heightForStaticVC; UIViewController *test = [[UIViewController alloc] init]; //Mod6: again setting the autoresizingMask test.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; test.view.backgroundColor = [UIColor grayColor]; test.view.frame = CGRectMake(0, yPosForStaticVC, 320, heightForStaticVC); UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button3 setTitle:@"View 3" forState:UIControlStateNormal]; button3.frame = CGRectMake(30.0, 30.0, 120.0, 50.0); [test.view addSubview:button3]; //Mod7: and again adding it as childVC of the rootVC [self addChildViewController:test]; [self.view addSubview:test.view]; [test didMoveToParentViewController:self]; } 

Of course, you can modify the calibration, positioning and autoresistor behavior as you like.

The result is as follows:

Tab1 PortraitTab2 Portrait

Tab1 landscape

Tab2 landscape

0
source

Source: https://habr.com/ru/post/1441385/


All Articles