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