xcode6.3 tab bar controller底部添加tab bar item无法并排显示

今天看啥 热点:
IOS开发之TabBarItem&NavigationBarItem,iostabbaritem  想必大家都用过微信,微信间的页面切换是如何做成的呢?接下来我们用storyboard结合着代码来模拟一下微信的视图控制模式。
  "工欲善其事,必先利其器",下面主要是对storyboard来进行我们项目框架的搭建的,必要时,用代码实现我们的页面效果。在IOS开发中常用的多视图间的切换大致有TabBarController, NavigationBarController, 和模态窗口。第一次接触模态的概念是在Web前端的内容中接触的。下面将会结合一个实际的效果来简单的介绍一下TabBar和NavigationBar, 然后说一下用我们的storyboard和纯代码如何配置我们的NavigationBar, 上一篇博客中提到了如何用Navigation来实现页面间的切换,接下来我们要和TabBarController结合起来,来做一个防微信的视图控制 。下面的视图控制器之间的关系是如何用storyboard拖出来的,在之前的博客中有所提及,在这小编就不赘述了。
  ViewController之间的关系大致入下图:
&1&- (IBAction)tapButton:(id)sender {
NSString * userName = [self.userName text];
NSString * password = self.password.
//模拟登陆
if ([userName isEqual:password] == YES && ![userName isEqual:@""] && ![password isEqual:@""]) {
//获取storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//由storyboardID获取TabBarViewController
UIViewController * myTabBarController = [storyboard instantiateViewControllerWithIdentifier:@"myTabBarController"];
//将界面跳转到我们的TabBarViewController
[self presentViewController:myTabBarController animated:YES completion:^{
    2.如何给我们新建的视图控制器关联我们新建的视图控制器类呢?其实在我们storyboard中还是蛮简单的,在storyboard选择我们要关联的视图控制器,在 Class中选中我们新建的视图控制器类即可。关联完以后我们就可以在相应的试图控制器类中实现我们的业务逻辑啦,操作如下:
//手动添加NavigationBar上得右侧按钮
//新建BarButtonItem1, 同时指定样式,和注册回调
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(tapBarButton)];
//新建BarButtonItem2, 同时指定样式,和注册回调
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self
action:@selector(tapBarButton)];
//往右侧添加一个按钮
self.navigationItem.rightBarButtonItem = item1;
//往右侧添加两个按钮
self.navigationItem.rightBarButtonItems = @[item1, item2];
//往左侧添加一个BarButton
self.navigationItem.leftBarButtonItem = item1;
//往左侧添加两个BarButton
self.navigationItem.leftBarButtonItems = @[item1, item2];
    (3)在storyboard给我们的NavigationBar设置title,或者titleView, 也是用拖拽的方式加配置相应的属性完成的,在这就不在赘述,下面给入用手动添加的方法,下面的代码是给我们的titleView赋值ImageView,我们还可以给他赋值别的控件,如UIButton等,代码如下:
//设置title的值
self.navigationItem.title = @"END";
//用给titleView设置图片
//新建imageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
//设置图片
UIImage * image = [UIImage imageNamed:@"topView.png"];
//图片加入ImageView
imageView.image =
//ImageView加入到navigationItem
self.navigationItem.titleView = imageV
  4.在各个视图间切换的时候下面的Tab Bar是存在的,或减少我们的可视空间,那么我们应如何隐藏掉呢?在我们的storyboard中只需要把Hide Bottom Bar on push选中即可,选中之后我们切换到该页面时,就不会显示Tab Bar了,如下图所示。
1 //点击退出按钮
2 - (IBAction)tapExit:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{}];
  总结: 上面的东西没有多高深的技术,用storyboard拖来拖去的(同时给出了纯代码的解决方案), 需要我们手写代码进行页面间跳转的地方我们再手写跳转。上面的各种ViewController关联以后,我们在给各个ViewController绑定实体的类后,就可以专心的实现我们每个View的业务逻辑的。在View中展示数据的时候,TableView用的也是蛮多的。 对于TableView如何使用,在后面的博客会进行详细的介绍。上面的Project用storyboard配置好以及实现相应的代码,运行效果如下:
在组装uitabbarcontroller的代码中来指定各个uitabbaritem.而不是在各个被包含的viewcontroller中UITabbarController *tabController=[[UITabbarController alloc] init];self.vc1=[[UIViewController alloc] init];slef.vc1.tabbarItem=......;self.vc2=[[UIViewController alloc] init];self.vc2.tabbarItem = ....;......NSArray *vcs=@[self.vc1,self.vc2,....];[tabController setViewControllers:vcs];
iOS5SDKXCODE4.2objective-C不过时间可能比较长,如果你有开发经验的话,我推荐你,找一个iOS研发公司的培训部门,做上4-5个项目。就行了,时间1个月左右!
暂无相关文章
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新在iOS的程序中,Tab&Bar的使用率很高,几个视图需要切换的时候,就用到tabbar。
今天的程序实现的效果是这样的,底部有几个tab Item,对应的有几个视图,切换tab Item,切换到对应的视图。
实现效果如下:
为了更好理解使用用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.
弹出窗口输入 rootController,点connect。
&6、添加代码
打开tabbarAppDelegate.m,在didFinishLaunchingWithOptions方法中添加代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];
[self.window addSubview:self.rootController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
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。
- (void)viewDidLoad
[super viewDidLoad];
NSLog(@"BlueViewController");
// Do any additional setup after loading the view from its nib.
代码获取:
阅读(...) 评论()Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Tab Bar Controller - Navigation Controller - TableView
I am having a bear of a time getting this to work.
I've looked at it so many ways and followed tutorials on how to set each of the controllers separately but none show how to combine them all.
super simple bare bones sample
- create new Tab Bar Application
- create a new FirstScreenViewController .h and .m class
- add the appropriate TableView methods (as explained in other articles)
- Create a new FirstScreen.xib with a UIView
- Drop a TableView onto the View
- set the files owner to point to FirstScreenViewController
- Save to project
- load up MainWindow.xib
- set the first tab to be a Navigation Controller
- set the first tabs xib to point to the FirstScreen.xib file
Save and run.
I see a TabBar window with two buttons.
the first button has a navigation controller toolbar up top and i can see the outlines of an empty Table View.
Looks great so far.
Load up my FirstScreenViewController.xib file.
Connect the view, tableview datasource and tableview delegate to point over to the Files Owner.
Run the project, hangs for a second then dumps me back to xcdoe with an unhandled error.
Take out the datasource and delegate connections and i'm back to see a blank tableview.
Any Ideas?
I know this is a long winded desription.. sorry.
I'm able to create a single page application with a tableview with no problem but when i dump this into a tabbar controller it craps out.
if you have any ideas please show me the light!
Post edited by BostonMerlin on December 2011
&b&I love being a dad, flying airplanes and writing code.&/b&&br /&
Follow me on Twitter: &a href=0
Have you pushed the tableview onto the navigation controller's stack?
you might try setting the tableViewControllers' delegates and datasources programatically just in case you're unintetionally doing something weird in interface builder.
I've done all of my development programatically so far, because I like to see things explicitly spelled out, not hidden behind a gui the way they are in IB.
Good luck!已有 83 人收藏
&&&&实现自定义TabBar控件,可以对TabBar样式进行定制,可以再tab上添加数字角标。可以快速实现类似新浪微博的视图布局。
&&&&小编注:感谢开发者@ 发布代码于。
测试环境:
&&&&[Code4App]编译测试,测试环境:Xcode 5.0, iOS 7.0 以上。
相关代码:
& (14552次查看,7114次下载)
& (12762次查看,3193次下载)
& (25014次查看,10979次下载)
& (15971次查看,3770次下载)
& (33972次查看,12206次下载)
& (19006次查看,6221次下载)
代码评论:
登录后方可评论
这个demo没有写完 tabbarviewcontroller 的功能也都没有
只是个VC 可能会错误的引导新手
登录后方可评论
楼主,配合视图控制器UINavigationController该怎么做?
登录后方可评论
如果要放视图控制器UINavigationController,应该怎么做?
登录后方可评论
@飞奔的蜗牛Lawliet : 同求。。
登录后方可评论
你好 我发现在IOS6下tabbar 的item都不显示,如何破?楼主
登录后方可评论
@沉迷 : 百度云
/s/1pJDKVkn
这个是新的...
登录后方可评论
@沉迷 : IOS6.0
登录后方可评论
加载 导航栏 之后 好像影藏tabbar 会 有一块空白 不可以用
登录后方可评论
@初見人生 : 兄弟,咋加的导航栏?
登录后方可评论
tab中的controller的下部分会被遮住,怎么才能不被遮住,要修改controller的高度吗
登录后方可评论
@ : 你好 我发现在IOS6.0 下tabbar 的item都不显示,如何破?楼主
登录后方可评论
@衡成飞 : 非常感谢作者的帮忙。
登录后方可评论
@ : 请问,如果如何隐藏下面的自定义tab bar,有的页面可能不需要。
登录后方可评论
@衡成飞 : 名字就是
登录后方可评论
@衡成飞 : 兄弟,有个问题想请教您,能否留下qq,加您一下。
登录后方可评论
@ : 好的,谢谢,有不懂的再请教您。
登录后方可评论
@衡成飞 : 对需要动态的修改控件的位置,不能把label的纵坐标设成一个具体的值,应该根据当前屏幕的尺寸来设置lable的纵坐标。
登录后方可评论
@ : 如果在例子中的viewcontroller1中下面加一个label,在ios7.0 4.0英寸的话可以正常显示,但是在iso6.0 3.5显示的话,则会被盖住。请问如何动态修改view,是修改里面控件的位置吗?
登录后方可评论
@衡成飞 : 1、需要修改viewcontroller中view的高度。2、3.5和4.0的屏应该是已经适配过了,下面云盘里的程序对ios 6.0也做了适配...
登录后方可评论
@衡成飞 : 3.5和4.0英寸大小如何控制底部显示呢
登录后方可评论
检查了一下,问题主要是出在这里:@property (nonatomic, weak) MALTabBar *tabB
weak换成strong,在真机上面就能正常显示了。。。。
登录后方可评论
@number1男儿当自强 :谢谢指出问题,当时也没在真机测试过也没发现...
登录后方可评论
表示在模拟器上面可以看到下面的tabbaritem,在真机上面,下面一片空白,,,,
登录后方可评论
由于时间关系当时上传的Demo有很多bug,但不知道代码如何更新,新版本放在百度云盘需要的同学可以在这里取
/s/1c0rfJS8
登录后方可评论
@ : 请问如何从第一个界面直接pushviewcontroller 啊
登录后方可评论
-控件分类-
-功能分类-
耗时 0.1244 秒

我要回帖

更多关于 xcode6 教程 的文章

 

随机推荐