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];