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

Posted

技术标签:

【中文标题】Objective-C:具有拆分视图的 UITabBarController【英文标题】:Objective-C: UITabBarController with splitted views 【发布时间】:2012-10-22 20:53:12 【问题描述】:

我对 Objective-C 很陌生,我正在尝试为 ios 编写我的第一个应用程序。这个想法很简单,但我在开始架构构建时已经失败了。

我想创建显示在多个选项卡上的不同视图,这些视图应该在加载视图时动态创建。此外,应用程序必须能够在运行时动态添加选项卡。选项卡视图不应覆盖整个屏幕,而应占顶视图的 2/3。底部剩下的 1/3 又被分成两个子视图,不打算用 tab 开关改变。

我所做的是创建一个 UIWindow、UITabBarController 和两个 UIViewController(用于两个选项卡)和一个(或如图中的两个)应该在底部。

到目前为止,我已经设法在不同的选项卡视图之间切换,但是当我尝试使用 CGMakeRect 将两个选项卡的 UIViewControllers 调整为任何大小时,它总是保持不变并覆盖整个屏幕。

在底部创建的子视图包含一个不可点击的按钮。也许是因为它被标签视图覆盖了。

谁能帮我建立这些视图?

非常感谢!

这是我的代码:

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];

【问题讨论】:

您以编程方式创建 GUI 真是太棒了! +1。但是,在未来,请不要写“Xcode development”或类似的东西,因为据您所知,Xcode 并不是真正必要的或与这种情况特别相关(您可以使用 nano 和 make 来编写应用程序。)。 遗憾的是,您可能会发现您无法做您想做的事情 - UITabBarController 必须(根据文档)是 rootViewController(您已经完成),但它特别没有不要与其他侵占其地盘的人相处融洽。您可以尝试将标签栏控制器嵌入到父 UIViewController 内的 ContentView 中,但如果这要进入应用商店,我不知道 Apple 以这种方式使用 UITabBarController 的检查/政策。 有可能,请看下面我的回答。这可能不是最漂亮的方式,但效果很好。 【参考方案1】:

实际上有一种方法可以实现这一点,这要归功于自定义容器视图控制器:

UITabBarController 替换为UIViewController 的自定义子类作为窗口的rootViewController。然后在它的viewDidLoad 方法(或其他地方,取决于您的需要)中,您几乎可以从上面添加您的确切代码,并进行一些小的修改。以下是viewDidLoad 方法的完整代码(我在修改后的行上方添加了 cmets):

- (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];


当然,您可以根据需要修改大小、定位和自动调整大小的行为。

结果如下:

【讨论】:

非常感谢,现在看起来很棒!

以上是关于Objective-C:具有拆分视图的 UITabBarController的主要内容,如果未能解决你的问题,请参考以下文章

设置拆分视图控制器 Xamarin.iOS

添加表格视图单元格在 iOS 中不起作用?

iOS、iPad - 具有相同主视图控制器和详细视图控制器的多个拆分视图控制器

使用故事板具有多个详细视图的拆分视图。看过一个例子/教程吗?

具有动态内容的 UICollectionView(每个单元格的视图数量可变)

Objective-C - 理解视图控制器