iOS基础-系统自带按钮样式- UIBarButtonSystemItem

系统按钮

除了图像与文字按钮,还有一个小型的系统按钮库,可以创建那些在许多应用程序中都可以见到的标准化的预定义按钮。系统按钮也是UIBarButtonItem对象,可以通过类的initWithBarButtonSystemItem方法来创建。如下例:

  1. UIBarButtonItem *myBookmarks = [ [ UIBarButtonItem alloc ]  
  2.         initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks  
  3.         target: self  
  4.         action: @selector(mySelector:)  
  5. ]; 

表3-2是目前支持的系统按钮,可以在UIBarButtonItem.h头文件中找到。

表3-2

按钮标识符

描    述

UIBarButtonSystemItemDone

蓝色文字按钮,标有“Done”

UIBarButtonSystemItemCancel

文字按钮,标有“Cancel”

UIBarButtonSystemItemEdit

文字按钮,标有“Edit”

UIBarButtonSystemItemSave

蓝色文字按钮,标有“Save”

UIBarButtonSystemItemAdd

图像按钮,上面有一个Å符号

UIBarButtonSystemItemFlexibleSpace

空白,占用空间大小可变

UIBarButtonSystemItemFixedSpace

空白占位符

UIBarButtonSystemItemCompose

图像按钮,上有一支笔和纸张

UIBarButtonSystemItemReply

图像按钮,上有一个回复箭头

UIBarButtonSystemItemAction

图像按钮,上有一个动作箭头

UIBarButtonSystemItemOrganize

图像按钮,上有一个文件夹以及向下箭头

UIBarButtonSystemItemBookmarks

图像按钮,上有书签图标

UIBarButtonSystemItemSearch

图像按钮,上有spotlight图标

UIBarButtonSystemItemRefresh

图像按钮,上有一个环形的刷新箭头

UIBarButtonSystemItemStop

图像按钮,上有一个停止记号X

UIBarButtonSystemItemCamera

图像按钮,上有一个照相机

UIBarButtonSystemItemTrash

图像按钮,上有一个垃圾桶

UIBarButtonSystemItemPlay

图像按钮,上有一个播放图标

UIBarButtonSystemItemPause

图像按钮,上有一个暂停图标

UIBarButtonSystemItemRewind

图像按钮,上有一个倒带图标

UIBarButtonSystemItemFastForward

图像按钮,上有一个快进图标


自定义视图按钮

与导航栏类似,按钮也可以按照自定义视图类来绘制,这样你就可以将任何一种其他类型的视图对象作为按钮来显示:

  1. UIBarButtonItem *customButton = [ [ UIBarButtonItem alloc ]  
  2.     initWithCustomView: myView ]; 

创建工具栏

将所有希望显示出来的按钮都添加到前面创建的buttons数组:

  1. [ buttons addObject: buttonImage ];  
  2. [ buttons addObject: buttonText ];  
  3. [ buttons addObject: myBookmarks ]; 

下一步,创建一个UIToolbar对象,并将你的按钮数组赋予工具栏作为项目列表:

  1. UIToolbar *toolbar = [ [ UIToolbar alloc ] init ];  
  2. [ toolbar setItems: buttons animated: YES ]; 

最后,将你的导航栏的标题视图替换为新创建的工具栏,就像替换成分段控件一样:

  1. self.navigationItem.titleView = toolbar; 

当导航栏显示出来时,工具栏就会出现在它的中央。

调整大小

工具栏会对加入的按钮套用默认大小。如果你希望调整工具栏,让它可以更干净利落地适应导航栏的大小,可以用sizeToFit方法:

  1. [ toolbar sizeToFit ]; 

工具栏的风格

就像许多其他基于视图的对象一样,UIToolbar也包含有一个风格属性,名为barStyle。这个属性可以用来调整工具栏的风格,令其与你为导航栏定义的风格相匹配:

  1. toolbar.barStyle = UIBarStyleDefault; 

可以将工具栏的风格设置为三种标准风格主题之一,这些主题也为大多数其他类型的栏状对象所使用,如表3-3所示。

表3-3

风    格

描    述

UIBarStyleDefault

默认风格;灰色背景、白色文字

UIBarStyleBlackOpaque

纯黑色背景、白色文字

UIBarStyleBlackTranslucent

透明黑色背景、白色文字

原文地址:https://www.cnblogs.com/dingjianjaja/p/5053189.html