原创blog,转载请注明出处
http://blog.csdn.net/hello_hwc?viewmode=contents
欢迎关注我的iOS SDK详解专栏
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
前言:ToolBar是一个常用的UIKit控件,常用来放置评论,收藏,分享等功能。本文模仿今日头条 App的文章部分的Toolbar
Demo效果
Demo 采用代理的方式传递点击事件,感兴趣的同学可以下载下来看看。可以根据需求进行订制化的修改。
链接
http://download.csdn.net/detail/hello_hwc/9023787
ToolBar的属性/方法
barStyle //bar 风格
分为两种
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
– setBackgroundImage:forToolbarPosition:barMetrics: //背景图片
- setShadowImage:forToolbarPosition://阴影图片
items//UIBarbuttonItems的数组,定义toolbar的图标
UIBarbuttonItem
具体的API就不讲了,BarbuttonItem主要分为四种
系统图标
initWithBarButtonSystemItem:target:action:
图片
initWithImage:style:target:action:
文字
initWithTitle:style:target:action:
自定义视图
initWithCustomView:
另外还要提到两种特殊的item,作为分隔符使用的
- UIBarButtonSystemItemFlexibleSpace //会自动调整宽度
- UIBarButtonSystemItemFixedSpace //固定宽度
举个简单的例子
代码
UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem * fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20;
UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithTitle:@"文字" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem * flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem * item3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"toobarDone"] style:UIBarButtonItemStylePlain target:nil action:nil];
self.toolBar.items = @[item1,fixedItem,item2,flexItem,item3];
简单讲解下Demo工程
- 评论的角标是采用runtime的机制,所以要调用set方法。
- 用代理+tag的方式来传递事件
- 仍然有些不完善的地方,不过不会再改了