4

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

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

我所做的是创建一个 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];
4

1 回答 1

0

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

将 替换为窗口的 rootViewController 的UITabBarController自定义子类。UIViewController然后在它的viewDidLoad方法中(或根据您的需要在其他地方)您从上面添加几乎您的确切代码,并进行一些小的修改。这是该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];

}

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

结果如下所示:

Tab1 纵向 Tab2 纵向

Tab1 横向

Tab2 横向

于 2012-10-31T18:21:14.860 回答