gpt4 book ai didi

Objective-C:具有 Split View的 UITabBarController

转载 作者:可可西里 更新时间:2023-11-01 06:16:23 26 4
gpt4 key购买 nike

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

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

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

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

在底部创建的 subview 包含一个无法点击的按钮。可能是因为它被选项卡 View 覆盖了。

任何人都可以帮助我如何建立这些观点吗?

非常感谢!

这是我的代码:

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

最佳答案

实际上,由于自定义容器 View Controller ,有一种方法可以实现这一点:

UITabBarController 替换为 UIViewController 的自定义子类作为窗口的 rootViewController。然后在它的 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 Portrait Tab2 Portrait

Tab1 Landscape

Tab2 Landscape

关于Objective-C:具有 Split View的 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13019682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com