自定义UITabBarController

发布于 2024-09-24 14:00:35 字数 882 浏览 0 评论 0原文

我想自定义 UITabBarController 的选项卡栏的外观和感觉。我想更改颜色、选择图标时的外观,而且最重要的是,我想减小自定义工具栏的大小。

我的方法和其中的障碍是:

A)我想到的第一个解决方案是创建我自己的viewController,它将像一个底部有按钮的UITabBarController一样,并将这个viewController添加到窗口中。一旦用户点击底部的按钮,将可视区域中的视图与新的 viewController 交换,该新的 viewController 对应于用户现在点击的按钮。

这种策略的问题是:由于我交换视图,相应的 viewController 将不会收到这些消息:

  • viewWillAppear
  • viewWillDisappear
  • viewDidAppear
  • viewDidDisappear

以及所有轮换事件

B)我可以在该线程中使用已接受的答案的方法: 自定义 UITabBarController 视图控制器和视图的问题

但是我的 tabBar 的高度与默认值不同。

由于上述原因,我无法使用这些方法。

话虽如此,我对“更多”选项卡没有特殊要求。我将只有 5 个选项卡将由选项卡栏显示,因此选项卡栏项目的重新排序超出了范围。

等待建议和想法。

I want to customize the look and feel of the tab bar of a UITabBarController. I want to change the colors, the way the icon looks when they are selected, and also, most important of all, I want to reduce the size of the custom toolbar.

My approaches for this and the hurdles in it are:

A) The first solution which came to my mind was to create my own viewController which will act like a UITabBarController with buttons in the bottom and add this viewController to the window. Once when user taps a button at the bottom, swap the view in the viewable area with the new viewController's which corresponds to the button now tapped by user.

The problem with this strategy is: since I swap view's the corresponding viewControllers will not get these messages:

  • viewWillAppear
  • viewWillDisappear
  • viewDidAppear
  • viewDidDisappear

And all the rotation events

B) I could have used the accepted answer's approach in this thread:
Custom UITabBarController Problems with View Controllers and Views

But my tabBar's height is not the same as the default.

Due to the cited reasons above, I cannot use those approaches.

Having said this, I have no special requirement of More tab. I will be having only 5 tabs which will be displayed by the tab bar and hence the re-ordering of tab bar items is out of scope.

Awaiting suggestions and ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-10-01 14:00:35

我从未尝试过这样的事情,但据我所知,您应该手动将这些消息发送到您的子视图控制器。

在适当的时刻将 -viewWill/Did(Dis)Appear 发送到正确的控制器应该没有问题。这也是 UITabBarController 所做的事情。

至于旋转事件:

  • shouldAutorotateToInterfaceOrientation: 中,将此消息转发给您的子控制器,并根据它们的返回值设置您的返回值(UITabBarController 仅返回 YES(如果其所有子控制器都针对请求的方向返回 YES)。

  • 转发 willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:willAnimateRotationToInterfaceOrientation:duration: 到子控制器(至少到当前可见的控制器)

  • 如果您已正确设置子控制器视图的自动调整大小蒙版,则当系统旋转自定义选项卡栏控制器视图时,您可以正确旋转并调整它们的大小。 (至少我认为它应该是这样工作的。)

同样,我不确定这是否有效。

I have never attempted something like this but as I see it, you are supposed to send those messages to your child view controllers manually.

It shouldn't be problem to send -viewWill/Did(Dis)Appear to the right controller at the appropriate moment. This is what UITabBarController does, too.

As for rotation events:

  • In shouldAutorotateToInterfaceOrientation:, forward this message to your child controllers and set your return value depending on their return values (UITabBarController only returns YES if all its child controllers return YES for the requested orientation).

  • Forward willRotateToInterfaceOrientation:duration:, didRotateFromInterfaceOrientation: and willAnimateRotationToInterfaceOrientation:duration: to the child controllers (at least to the currently visible one) when you receive them.

  • If you have set the autoresizing masks of your child controllers' views correctly, they you rotate and resize correctly when the system rotates your custom tab bar controller's view. (At least I think that's how it should work.)

Again, I'm not sure if this will work.

荒路情人 2024-10-01 14:00:35

您可以使用 CGRect make 实现以下代码来创建自定义选项卡栏,该代码用于创建自定义选项卡栏。进一步的代码用于创建自定义选项卡栏

-(void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    tabBarController.delegate = self;
    tabBarController = [[UITabBarController alloc] init];

    mainDashBoard = [[DashBoard alloc] initWithNibName:@"DashBoard" bundle:nil];
    mainSearchView = [[SearchView alloc] initWithNibName:@"SearchView" bundle:nil];
    mainMoreView = [[MoreView alloc] initWithNibName:@"MoreView" bundle:nil];

    UINavigationController *nvCtr0 = [[[UINavigationController alloc] init] autorelease];
    UINavigationController *nvCtr1 = [[[UINavigationController alloc] initWithRootViewController:mainDashBoard] autorelease];
    UINavigationController *nvCtr2 = [[[UINavigationController alloc] initWithRootViewController:mainSearchView] autorelease];
    UINavigationController *nvCtr3 = [[[UINavigationController alloc] initWithRootViewController:mainMoreView] autorelease];
    UINavigationController *nvCtr4 = [[[UINavigationController alloc] init] autorelease];//[[[UINavigationController alloc] initWithRootViewController:nil] autorelease];

    tabBarController.viewControllers = [NSArray arrayWithObjects:nvCtr0,nvCtr1,nvCtr2,nvCtr3,nvCtr4,nil];

    nvCtr0.tabBarItem.enabled = NO;
    nvCtr4.tabBarItem.enabled = NO;

    [window tabBarController.view];
}

You can implement the following code for the creating the custom tab bar in that use to images using the CGRect make.further code is use for the creating the custom tab bar

-(void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    tabBarController.delegate = self;
    tabBarController = [[UITabBarController alloc] init];

    mainDashBoard = [[DashBoard alloc] initWithNibName:@"DashBoard" bundle:nil];
    mainSearchView = [[SearchView alloc] initWithNibName:@"SearchView" bundle:nil];
    mainMoreView = [[MoreView alloc] initWithNibName:@"MoreView" bundle:nil];

    UINavigationController *nvCtr0 = [[[UINavigationController alloc] init] autorelease];
    UINavigationController *nvCtr1 = [[[UINavigationController alloc] initWithRootViewController:mainDashBoard] autorelease];
    UINavigationController *nvCtr2 = [[[UINavigationController alloc] initWithRootViewController:mainSearchView] autorelease];
    UINavigationController *nvCtr3 = [[[UINavigationController alloc] initWithRootViewController:mainMoreView] autorelease];
    UINavigationController *nvCtr4 = [[[UINavigationController alloc] init] autorelease];//[[[UINavigationController alloc] initWithRootViewController:nil] autorelease];

    tabBarController.viewControllers = [NSArray arrayWithObjects:nvCtr0,nvCtr1,nvCtr2,nvCtr3,nvCtr4,nil];

    nvCtr0.tabBarItem.enabled = NO;
    nvCtr4.tabBarItem.enabled = NO;

    [window tabBarController.view];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文