iPhone NavigationBar和UIToolbar基础

navigation bar 分为三个部分,左按钮,右按钮和中间的View.

在Controller中可以通过以三个方式来引用:

 

  • self . navigationItem. titleView
  • self . navigationItem. leftBarButtonItem
  • self . navigationItem. rightBarButtonItem
左右按钮可以使用UIBarButtonItem来构造. 他默认有很多种按钮可攻选择:
Java代码 
  1. typedef  enum  {  
  2.     UIBarButtonSystemItemDone,  
  3.     UIBarButtonSystemItemCancel,  
  4.     UIBarButtonSystemItemEdit,    
  5.     UIBarButtonSystemItemSave,    
  6.     UIBarButtonSystemItemAdd,  
  7.     UIBarButtonSystemItemFlexibleSpace,  
  8.     UIBarButtonSystemItemFixedSpace,  
  9.     UIBarButtonSystemItemCompose,  
  10.     UIBarButtonSystemItemReply,  
  11.     UIBarButtonSystemItemAction,  
  12.     UIBarButtonSystemItemOrganize,  
  13.     UIBarButtonSystemItemBookmarks,  
  14.     UIBarButtonSystemItemSearch,  
  15.     UIBarButtonSystemItemRefresh,  
  16.     UIBarButtonSystemItemStop,  
  17.     UIBarButtonSystemItemCamera,  
  18.     UIBarButtonSystemItemTrash,  
  19.     UIBarButtonSystemItemPlay,  
  20.     UIBarButtonSystemItemPause,  
  21.     UIBarButtonSystemItemRewind,  
  22.     UIBarButtonSystemItemFastForward,  
  23.     UIBarButtonSystemItemUndo,      // available in iPhone 3.0   
  24.     UIBarButtonSystemItemRedo,      // available in iPhone 3.0   
  25. } UIBarButtonSystemItem;  
 
通过构造初始化可以设置这些按钮的种类:
Java代码 
  1. [[[UIBarButtonItem alloc]   
  2.              initWithBarButtonSystemItem:UIBarButtonSystemItemPlay  
  3.              target:nil action:NULL] autorelease];  
 
除此之外,UIBarButtonItem还有几种外观风格:
Java代码 
  1. typedef  enum  {  
  2.     UIBarButtonItemStylePlain,    // shows glow when pressed   
  3.     UIBarButtonItemStyleBordered,  
  4.     UIBarButtonItemStyleDone,  
  5. } UIBarButtonItemStyle;  
 
当然按钮有风格之分, navigation bar 也有几种可选风格:
Java代码 
  1. typedef  enum  {  
  2.     UIBarStyleDefault          = 0 ,  
  3.     UIBarStyleBlack            = 1 ,  
  4.       
  5.     UIBarStyleBlackOpaque      = 1  // Deprecated. Use UIBarStyleBlack   
  6.     UIBarStyleBlackTranslucent = 2 // Deprecated. Use UIBarStyleBlack and set the translucent property to YES   
  7. } UIBarStyle;  
 
通过:navigationController . navigationBar . barStyle  =  UIBarStyleBlackTranslucent这样的语句就可以设置了.

这个UIBarStyle的风格还适用于UIToolbar. 并且UIToolBar中也是可以放置UIBarButtonItem的.
Java代码 
  1. NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity: 4 ];  
  2.       
  3.     UIBarButtonItem *flexibleSpaceItem;  
  4.     flexibleSpaceItem = [[[UIBarButtonItem alloc]   
  5.                           initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  6.                           target:nil action:NULL] autorelease];   
  7.     [buttons addObject:flexibleSpaceItem];  
  8.     [flexibleSpaceItem release];  
  9.       
  10.     UIBarButtonItem *item;  
  11.     item = [[UIBarButtonItem alloc]   
  12.             initWithImage:[UIImage imageNamed:@"down.png" ]  
  13.             style:UIBarButtonItemStylePlain   
  14.             target:self   
  15.             action:@selector (decrement:)];  
  16.     [buttons addObject:item];  
  17.     [item release];  
  18.       
  19.     item = [[UIBarButtonItem alloc]   
  20.             initWithImage:[UIImage imageNamed:@"up.png" ]  
  21.             style:UIBarButtonItemStylePlain target:self   
  22.             action:@selector (increment:)];  
  23.     [buttons addObject:item];  
  24.     [item release];  
  25.       
  26.     item = [[[UIBarButtonItem alloc]   
  27.              initWithBarButtonSystemItem:UIBarButtonSystemItemPlay  
  28.              target:nil action:NULL] autorelease];    
  29.     [buttons addObject:item];  
  30.     [item release];  
  31.       
  32.       
  33.     flexibleSpaceItem = [[[UIBarButtonItem alloc]   
  34.                           initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  35.                           target:nil action:NULL] autorelease];   
  36.   
  37.     [buttons addObject:flexibleSpaceItem];  
  38.     [flexibleSpaceItem release];  
  39.       
  40.       
  41.     UIToolbar *toolbar = [[UIToolbar alloc] init];   
  42.     toolbar.barStyle = UIBarStyleBlackOpaque;  
  43.     [toolbar setItems:buttons animated:YES];  
  44.     [toolbar sizeToFit];  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值