天天看点

iOS开发中标签控制器的使用——UITabBarController

        与导航控制器相类似,标签控制器也是用于管理视图控制器的一个ui控件,在其内部封装了一个标签栏,与导航不同的是,导航的管理方式是纵向的,采用push与pop切换控制器,标签的管理是横向的,通过标签的切换来改变控制器,一般我们习惯将tabbar作为应用程序的根视图控制器,在其中添加导航,导航中在对viewcontroller进行管理。

        通过如下的步骤,我们可以很简便的创建一个tabbarcontroller:

<a href="http://my.oschina.net/u/2340880/blog/529970#">?</a>

1

2

3

4

5

6

7

8

9

10

11

12

13

<code>uitabbarcontroller * tabbar= [[uitabbarcontroller alloc]init];</code>

<code>    </code><code>nsmutablearray * controllerarray = [[nsmutablearray alloc]init];</code>

<code>    </code><code>for</code> <code>(</code><code>int</code> <code>i=0; i&lt;4; i++) {</code>

<code>        </code><code>uiviewcontroller * con = [[uiviewcontroller alloc]init];</code>

<code>        </code><code>[con loadviewifneeded];</code>

<code>        </code><code>con.view.backgroundcolor = [uicolor colorwithred:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];</code>

<code>        </code><code>con.tabbaritem.image = [uiimage imagenamed:@</code><code>"btn_publish_face_a.png"</code><code>];</code>

<code>        </code><code>con.tabbaritem.title=[nsstring stringwithformat:@</code><code>"%d"</code><code>,i+1];</code>

<code>        </code><code>con.title = [nsstring stringwithformat:@</code><code>"%d"</code><code>,i+1];</code>

<code>        </code><code>[controllerarray addobject:con];</code>

<code>    </code><code>}</code>

<code>    </code><code>tabbar.viewcontrollers = controllerarray;</code>

<code>    </code><code>[self presentviewcontroller:tabbar animated:yes completion:nil];</code>

iOS开发中标签控制器的使用——UITabBarController

通过点击下面的标签按钮,可以很方便的切换控制器。如果我们的控制器数超过4个,系统会被我们创建一个more的导航,并且可以通过系统自带的编辑来调整控制器的顺序,如下:

iOS开发中标签控制器的使用——UITabBarController

14

15

<code>//管理的viewcontroller数组</code>

<code>@property(nullable, nonatomic,copy) nsarray&lt;__kindof uiviewcontroller *&gt; *viewcontrollers;</code>

<code>- (</code><code>void</code><code>)setviewcontrollers:(nsarray&lt;__kindof uiviewcontroller *&gt; * __nullable)viewcontrollers animated:(</code><code>bool</code><code>)animated;</code>

<code>//选中的viewcontrolle</code>

<code>@property(nullable, nonatomic, assign) __kindof uiviewcontroller *selectedviewcontroller;</code>

<code>//通过编号设置选中viewcontroller</code>

<code>@property(nonatomic) nsuinteger selectedindex;</code>

<code>//当viewcontroller大于4个时,获取"更多"标签的导航控制器</code>

<code>@property(nonatomic, readonly) uinavigationcontroller *morenavigationcontroller; </code>

<code>//这个属性设置的是可以进行自定义排列顺序的视图控制器,如上面第二张图中的,默认是全部</code>

<code>@property(nullable, nonatomic, copy) nsarray&lt;__kindof uiviewcontroller *&gt; *customizableviewcontrollers;</code>

<code>//标签控制器中分装的标签栏</code>

<code>@property(nonatomic,readonly) uitabbar *tabbar ns_available_ios(3_0);</code>

<code>//代理</code>

<code>@property(nullable, nonatomic,weak) id&lt;uitabbarcontrollerdelegate&gt; delegate;</code>

        通过自定义标签栏的一些属性,使我们可以更加灵活的使用tabbar。

设置标签:

<code>@property(nullable,nonatomic,copy) nsarray&lt;uitabbaritem *&gt; *items;  </code>

<code>//设置选中的标签    </code>

<code>@property(nullable,nonatomic,assign) uitabbaritem *selecteditem; </code>

<code>- (</code><code>void</code><code>)setitems:(nullable nsarray&lt;uitabbaritem *&gt; *)items animated:(</code><code>bool</code><code>)animated;</code>

设置自定义标签顺序:

<code>//调用这个方法会弹出一个类似上面第二张截图的控制器,我们可以交换标签的布局顺序</code>

<code>- (</code><code>void</code><code>)begincustomizingitems:(nsarray&lt;uitabbaritem *&gt; *)items;  </code>

<code>//完成标签布局</code>

<code>- (</code><code>bool</code><code>)endcustomizinganimated:(</code><code>bool</code><code>)animated;   </code>

<code>//是否正在自定义标签布局</code>

<code>- (</code><code>bool</code><code>)iscustomizing;</code>

设置tabbar颜色相关:

<code>//设置渲染颜色,会影响选中字体和图案的渲染</code>

<code>@property(null_resettable, nonatomic,strong) uicolor *tintcolor;</code>

<code>//设置导航栏的颜色</code>

<code>@property(nullable, nonatomic,strong) uicolor *bartintcolor;</code>

设置背景图案:

<code>//设置导航栏背景图案</code>

<code>@property(nullable, nonatomic,strong) uiimage *backgroundimage;</code>

<code>//设置选中一个标签时,标签背后的选中提示图案 这个会出现在设置的item图案的后面</code>

<code>@property(nullable, nonatomic,strong) uiimage *selectionindicatorimage;</code>

<code>//设置阴影的背景图案</code>

<code>@property(nullable, nonatomic,strong) uiimage *shadowimage</code>

tabbar中标签的宏观属性:

<code>//设置标签item的位置模式</code>

<code>@property(nonatomic) uitabbaritempositioning itempositioning;</code>

<code>//枚举如下</code>

<code>typedef</code> <code>ns_enum(nsinteger, uitabbaritempositioning) {</code>

<code>    </code><code>uitabbaritempositioningautomatic,</code><code>//自动</code>

<code>    </code><code>uitabbaritempositioningfill,</code><code>//充满</code>

<code>    </code><code>uitabbaritempositioningcentered,</code><code>//中心</code>

<code>} ns_enum_available_ios(7_0);</code>

<code>//设置item宽度</code>

<code>@property(nonatomic) cgfloat itemwidth;</code>

<code>//设置item间距</code>

<code>@property(nonatomic) cgfloat itemspacing;</code>

与导航栏类似,也可以设置tabbar的风格和透明效果:

<code>//风格 分黑白两种</code>

<code>@property(nonatomic) uibarstyle barstyle;</code>

<code>//是否透明效果</code>

<code>@property(nonatomic,getter=istranslucent) </code><code>bool</code> <code>translucent;</code>

<code>//选中标签时调用</code>

<code>- (</code><code>void</code><code>)tabbar:(uitabbar *)tabbar didselectitem:(uitabbaritem *)item;</code>

<code>//将要开始编辑标签时</code>

<code>- (</code><code>void</code><code>)tabbar:(uitabbar *)tabbar willbegincustomizingitems:(nsarray&lt;uitabbaritem *&gt; *)items;          </code><code>//已经开始编辑标签时         </code>

<code>- (</code><code>void</code><code>)tabbar:(uitabbar *)tabbar didbegincustomizingitems:(nsarray&lt;uitabbaritem *&gt; *)items;           </code>

<code>//将要进入编辑状态时</code>

<code>- (</code><code>void</code><code>)tabbar:(uitabbar *)tabbar willendcustomizingitems:(nsarray&lt;uitabbaritem *&gt; *)items changed:(</code><code>bool</code><code>)changed; </code>

<code>//已经进入编辑状态时</code>

<code>- (</code><code>void</code><code>)tabbar:(uitabbar *)tabbar didendcustomizingitems:(nsarray&lt;uitabbaritem *&gt; *)items changed:(</code><code>bool</code><code>)changed;</code>

        和navigationitem类似,标签栏上的item也可以自定义,一些方法如下。

初始化方法:

<code>//通过标题和图案进行创建</code>

<code>- (instancetype)initwithtitle:(nullable nsstring *)title image:(nullable uiimage *)image tag:(nsinteger)tag;</code>

<code>- (instancetype)initwithtitle:(nullable nsstring *)title image:(nullable uiimage *)image selectedimage:(nullable uiimage *)selectedimage;</code>

<code>//创建系统类型的</code>

<code>- (instancetype)initwithtabbarsystemitem:(uitabbarsystemitem)systemitem tag:(nsinteger)tag;</code>

uitabbarsystemitem的枚举如下:

<code>typedef</code> <code>ns_enum(nsinteger, uitabbarsystemitem) {</code>

<code>    </code><code>uitabbarsystemitemmore,</code><code>//更多图标</code>

<code>    </code><code>uitabbarsystemitemfavorites,</code><code>//最爱图标</code>

<code>    </code><code>uitabbarsystemitemfeatured,</code><code>//特征图标</code>

<code>    </code><code>uitabbarsystemitemtoprated,</code><code>//高级图标</code>

<code>    </code><code>uitabbarsystemitemrecents,</code><code>//最近图标</code>

<code>    </code><code>uitabbarsystemitemcontacts,</code><code>//联系人图标</code>

<code>    </code><code>uitabbarsystemitemhistory,</code><code>//历史图标</code>

<code>    </code><code>uitabbarsystemitembookmarks,</code><code>//图书图标</code>

<code>    </code><code>uitabbarsystemitemsearch,</code><code>//查找图标</code>

<code>    </code><code>uitabbarsystemitemdownloads,</code><code>//下载图标</code>

<code>    </code><code>uitabbarsystemitemmostrecent,</code><code>//记录图标</code>

<code>    </code><code>uitabbarsystemitemmostviewed,</code><code>//全部查看图标</code>

<code>};</code>

uitabbaritem常用属性:

<code>//设置选中图案</code>

<code>@property(nullable, nonatomic,strong) uiimage *selectedimage;</code>

下面这个属性可以设置item的头标文字:

<code> </code><code>con.tabbaritem.badgevalue = @</code><code>"1"</code><code>;</code>

iOS开发中标签控制器的使用——UITabBarController

<code>//设置标题的位置偏移</code>

<code>@property (nonatomic, readwrite, assign) uioffset titlepositionadjustment;</code>

由于uitabbaritem是继承于uibaritem,还有下面这个属性可以设置使用:

<code>//标题</code>

<code>@property(nullable, nonatomic,copy)             nsstring    *title;   </code>

<code>//图案    </code>

<code>@property(nullable, nonatomic,strong)           uiimage     *image;  </code>

<code>//横屏时的图案      </code>

<code>@property(nullable, nonatomic,strong)           uiimage     *landscapeimagephone;</code>

<code>//图案位置偏移</code>

<code>@property(nonatomic)                  uiedgeinsets imageinsets; </code>

<code>//横屏时的图案位置偏移</code>

<code>@property(nonatomic)                  uiedgeinsets landscapeimagephoneinsets ;</code>

<code>//设置和获取标题的字体属性</code>

<code>- (</code><code>void</code><code>)settitletextattributes:(nullable nsdictionary&lt;nsstring *,id&gt; *)attributes forstate:(uicontrolstate)state;</code>

<code>- (nullable nsdictionary&lt;nsstring *,id&gt; *)titletextattributesforstate:(uicontrolstate)state;</code>