iPhone:我如何构建自己的TabBar? [英] iPhone: How can I build my own TabBar?

查看:137
本文介绍了iPhone:我如何构建自己的TabBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我对普通iphone标签栏无法提供的标签栏有一些要求,我需要自己构建。

Because I have some requirements for a tabbar that the normal iphone tabbar cannot provide, I am needing to build my own.

构建我的最佳方法是什么自己的标签栏,具体来说,如何以正确的方式在我的主视图控制器中添加/删除(显示/隐藏)视图,同时考虑到子视图的内存和最佳实践?

What is the best way to build my own tabbar, specifically, how to add/remove (show/hide) views in my main view controller in the right way, taking into account memory and best practices for subviews?

推荐答案

正如我在其他地方所说的那样,摆脱 UIKit提供的核心导航类几乎是永远一个好主意。您认为哪种类型的应用程序要求值得完全自定义标签栏类?通过子类化,分类或使用图层,几乎总是可以实现必要的自定义。

As I've stated elsewhere, it's almost never a good idea to get rid of the core navigational classes that are provided by UIKit. What type of application requirements do you have that you think merit a completely custom tab bar class? It's almost always possible to achieve the necessary customizations by either subclassing, categorizing, or making use of layers.

UPDATE 1 :所以这就是我的意思在我的一些应用程序中做了自定义标签栏实现。

UPDATE 1: So here's what I did in some of my apps to get a custom tab bar implementation.


  1. 创建 UITabBar

  2. 向您的自定义子类添加一个名为 -updateTabBarImageForViewControllerIndex的方法:

  3. 在Interface Builder中,将标签栏控制器的标签栏的类更改为自定义子类

  4. 在任何符合标签栏控制器委托的类中(例如,您的应用程序委托),实现 -tabBarController:shouldSelectViewController:并在自定义标签栏子类上调用 -updateTabBarImageForViewControllerIndex:

  1. Create a subclass of UITabBar
  2. Add a method to your custom subclass called something like -updateTabBarImageForViewControllerIndex:
  3. In Interface Builder, change the class of your tab bar controller's tab bar to your custom subclass
  4. In whatever class conforms to your tab bar controller's delegate (e.g., your app delegate), implement -tabBarController:shouldSelectViewController: and call -updateTabBarImageForViewControllerIndex: on your custom tab bar subclass

基本上,每次标签栏控制器要切换视图控制器时,都要通知标签栏子类。发生这种情况时,请确定需要为标签栏选择的图像。您的标签栏应该有 n 图像,一个用于每个标签的选定状态。实际上可以捏造 UITabBarItem 的实现,并且可以处理单个图像,但这需要更多的工作。

Basically, you want to notify your tab bar subclass every time the tab bar controller is about to switch view controllers. When this happens, determine what image you need to choose for your tab bar. You should have n images for your tab bar, one for the selected state of each tab. It's possible to actually fudge the implementation of UITabBarItem and just work with individual images, but it's a little more work.

// MyAppDelegate.m

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    // Determine the index based on the selected view controller

    NSUInteger viewControllerIndex = ...;

    [(MyTabBar *)tabBarController.tabBar updateTabBarImageForViewControllerIndex:viewControllerIndex];

    return YES;
}

// MyTabBar.m

- (void)updateTabBarImageForViewControllerIndex:(NSUInteger)index
{
    // Determine the image name based on the selected view controller index

    self.selectedTabBarImage = [UIImage imageNamed:...];

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, self.selectedTabBarImage.CGImage);
}

更新2 :现在我想到了更多的是,你实际上可以(并且应该)在没有继承 UITabBar 的情况下完成你想要实现的目标。导入< QuartzCore / QuartzCore.h> 并利用图层内容。 :)

UPDATE 2: Now that I think about it more, you actually could (and should) get away with what you're trying to achieve without subclassing UITabBar at all. Import <QuartzCore/QuartzCore.h> and take advantage of layer contents. :)

// MyAppDelegate.m

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    // Determine the image name based on the selected view controller

    CGImageRef newTabBarImageRef = [[UIImage imageNamed:...] CGImage];
    tabBarController.tabBar.layer.contents = (id)newTabBarImageRef;

    return YES;
}

这篇关于iPhone:我如何构建自己的TabBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆