简体   繁体   English

Objective-C:具有拆分视图的UITabBarController

[英]Objective-C: UITabBarController with splitted views

I'm quite new to Objective-C and I'm trying to write my first app for iOS. 我是Objective-C的新手,我正在尝试编写我的第一个iOS应用程序。 The idea is quite simple but I have failed already in the beginning architecture construction. 这个想法非常简单,但我在开始构建时已经失败了。

I would like to create different views displayed over several tabs, which should be created dynamically when the view is loaded. 我想在多个选项卡上创建不同的视图,这些视图应在加载视图时动态创建。 Additionally, the app must have the ability to add tabs dynamically at runtime. 此外,应用程序必须能够在运行时动态添加选项卡。 Tab views should not run over the entire screen but should fill 2/3 of the top view. 标签视图不应在整个屏幕上运行,但应填充顶视图的2/3。 The remaining 1/3 at the bottom is again divided into two subviews, which are not intended to be changed with tab switches. 底部的剩余1/3再次分为两个子视图,这些子视图不打算使用制表符开关进行更改。

What I've done is to create a UIWindow, UITabBarController, and two UIViewControllers (for two tabs) and one (or two as in the figure) which is supposed to be at the bottom. 我所做的是创建一个UIWindow,UITabBarController和两个UIViewControllers(用于两个选项卡)和一个(或图中的两个),它应该位于底部。

So far I've managed to switch between different tab views, but as soon as I try to resize UIViewControllers for both tabs with CGMakeRect to any size it always stays the same and covers the entire screen. 到目前为止,我已经设法在不同的选项卡视图之间切换,但是一旦我尝试使用CGMakeRect将两个选项卡的UIViewControllers调整为任何大小,它总是保持不变并覆盖整个屏幕。

The subview created at the bottom contains a button which is somehow not clickable. 底部创建的子视图包含一个不可点击的按钮。 Maybe because it's covered from the tab views. 也许是因为它从标签视图中被覆盖了。

Can anyone give me a little help how I can build up those views? 谁能给我一点帮助我如何建立这些观点?

Thanks a lot! 非常感谢!

Here is be 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];

Actually there is a way to achieve that thanks to custom container view controllers: 实际上有一种方法可以实现这一点,这要归功于自定义容器视图控制器:

Replace the UITabBarController with a custom subclass of UIViewController as rootViewController of the window. UITabBarController替换为UIViewController的自定义子类,作为窗口的rootViewController。 Then in it's viewDidLoad method (or somewhere else depending on your needs) you add almost your exact code from above with some minor modifications. 然后在它的viewDidLoad方法中(或根据您的需要在其他地方),您可以从上面添加几乎您的确切代码并进行一些小修改。 Here's the complete code for the viewDidLoad method (I've added comments above the modified lines): 这是viewDidLoad方法的完整代码(我在修改后的行上面添加了注释):

- (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's view
    //if needed you can also set it's 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's 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 sizing, positioning and autoresizing behavior as you want. 当然,您可以根据需要修改大小调整,定位和自动调整行为。

The result looks like this: 结果如下:

Tab1肖像Tab2肖像

Tab1风景

Tab2风景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM