iPhone NavigationBar和UIToolbar基础

本文详细介绍了iOS开发中导航栏(navigationBar)的组成部分及其使用方法,包括左按钮、右按钮和中间视图的设置。同时,文章还列举了系统提供的多种按钮样式和导航栏风格,并展示了如何在控制器中通过代码进行配置。此外,文中还提到了工具栏(UIToolbar)的使用方法及UIBarButtonItem的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

 

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

这个UIBarStyle的风格还适用于UIToolbar. 并且UIToolBar中也是可以放置UIBarButtonItem的.
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];
	
	UIBarButtonItem *flexibleSpaceItem;
	flexibleSpaceItem = [[[UIBarButtonItem alloc] 
						  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
						  target:nil action:NULL] autorelease];	
	[buttons addObject:flexibleSpaceItem];
	[flexibleSpaceItem release];
	
	UIBarButtonItem *item;
	item = [[UIBarButtonItem alloc] 
			initWithImage:[UIImage imageNamed:@"down.png"]
			style:UIBarButtonItemStylePlain 
			target:self 
			action:@selector(decrement:)];
	[buttons addObject:item];
	[item release];
	
	item = [[UIBarButtonItem alloc] 
			initWithImage:[UIImage imageNamed:@"up.png"]
			style:UIBarButtonItemStylePlain target:self 
			action:@selector(increment:)];
	[buttons addObject:item];
	[item release];
	
	item = [[[UIBarButtonItem alloc] 
			 initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
			 target:nil action:NULL] autorelease];	
	[buttons addObject:item];
	[item release];
	
	
	flexibleSpaceItem = [[[UIBarButtonItem alloc] 
						  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
						  target:nil action:NULL] autorelease];	

	[buttons addObject:flexibleSpaceItem];
	[flexibleSpaceItem release];
	
	
	UIToolbar *toolbar = [[UIToolbar alloc] init]; 
	toolbar.barStyle = UIBarStyleBlackOpaque;
	[toolbar setItems:buttons animated:YES];
	[toolbar sizeToFit];
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值