iOS 关于创建超过5个 tabBar 的方法!

iOS 关于创建超过5个 tabBar 的方法!,第1张

如果使用系统的tabBar 最多只能显示5个,所以想要展示超过5个 就只能自己想办法弄喽!我用的办法是:

1:先把系统的tabBar 隐藏掉  [ self .tabBar setHidden: YES ]  ps:在继承了UITabBarController 的tabBar控制器里面! 

2:自定义个view添加到自定义的tabBar控制器里面,上面创建你想要的个数的按钮!然后放在tabBar的位置上!

3:按钮点击的时候切换 tabBar 控制器的 selectIndex !至于按钮的图片文字之类的 使用按钮的系统类型UIButtonTypeCustom 设置普通和选中的图片以及文字 文字颜色等!

如图:

为了更好理解使用用tabbar和切换视图,我们创建一个Empty Application。

   1、 打开Xcode ,新建项目

   2、 创建View Controller

在项目上按花键+N创建新文件,创建 Objective-C class 文件,按Next按钮,subClass 选UIViewController 。勾选上xib选项

以同样方式创建另外三个ViewController ,RedViewController ,GreyViewController,YellowViewController。四个View准备好了。那么Tabbar呢?

   3、 创建TabBarController.xib文件,选择创建Empty文件

这时候你发现创建的xib文件是空白的,不用慌,去右下角控件栏中把TabBar Controller拖过来就Ok了。

4、 关联TabBarController.xib ,tabbarAppDelegate这两个文件

在上图中选择File’s Owner,打开Identity Inspector,在Class一栏选择tabbarAppDelegate

这样,我们就可以创建TabBarController.xib 文件指向tabbarAppDelegate 文件的Outlet映射了。

5、 在Xcode中的工具栏的View菜单找到 打开Assistant Editor,使tabbarAppDelegate.h和TabBarController.xib 同时打开。

在xib文件上按住control键,往tabbarAppDelegate.h,创建Outlet.

d出窗口输入 rootController,点connect。

   6、 添加代码

打开tabbarAppDelegate.m,在didFinishLaunchingWithOptions方法中添加代码:

1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

2. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]

3. // Override point for customization after application launch.

4. [[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil]

5. [self.window addSubview:self.rootController.view]

6. self.window.backgroundColor = [UIColor whiteColor]

7. [self.window makeKeyAndVisible]

8. return YES

   7、 往TabBarController.xib上添加Tab Bar Item,

把控件栏上的Tab Bar Item控件往TabBarController.xib上拖拽即可,一个放4个。

   8 、关联Tab Bar Item和***ViewController。

选择其中一个Tab Bar Item,在右上角打开Identity Inspector,在Class中选择BlueViewController:

然后,打开Attribute,在NIB Name选择BlueViewController:

其他3个tab item重复类似的 *** 作,选中对应的ViewController,这样在切换Tab标签时,就可以切换到对应的页面。

9、 设置tab item的属性

选中其中一个tab item ,会在右上角的属性栏里看到如下信息

Badge是红色圈圈里面有数字 ,表示有多少条信息的属性

Identifier 是tab item的样式,选custom是自定义,下面的是系统的样式。我选了其中四种。

bar ITem 的title image在custom的样式下能设置。

10 、剩下的3个Tab Item也做类似的设置即可。

现在基本完工,运行看看结果如何。好吧,其实和第一第二个图是一样的`,这里就不放了。

11 、在viewDidLoad方法加Log观察切换View

可以加写日志看看对应的View是什么时候运行的。第一个运行的View是BlueViewController,点击其他的tab项时,加载其他的view,加载一次之后下次点击不再调用viewDidLoad。

1.- (void)viewDidLoad

2. [super viewDidLoad]

3. NSLog(@"BlueViewController")

4. // Do any additional setup after loading the view from its nib.

首先我需要确定这个加号是button还是一个imageview。

如果是button直接add即可代码如下。

[button addTarget:self action:@selector(addBankCardAction) forControlEvents:UIControlEventTouchUpInside]

如果是一个imageview建议添加手势

//首先开启imageview的交互 

[imageView1 setUserInteractionEnabled:YES]

//创建一个手势

UITapGestureRecognizer *singleTap =

  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(whenClickImage)]

//将手势添加到iamgeview上

      [imageView addGestureRecognizer:singleTap]

//在事件中做你想做的。

-(void)whenClickImage

{

NSLog(@"you click image")

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11816073.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存