1.在UINavigationController导航控制器中存在UIToolBar的,而且默认时被隐藏的。
- 需要设置self.navigationController.toolbarHidden = NO;
(建议最好在viewWillApper:设置) - 然后创建UIBarButtonItem,需要用到什么类型就加什么,系统自带了许多样式让你选择(还是自定义好看,符合国人的使用)
UIBarButtonSystemItemDone -
UIBarButtonSystemItemCancel -
UIBarButtonSystemItemEdit -
UIBarButtonSystemItemSave -
UIBarButtonSystemItemAdd -
UIBarButtonSystemItemFlexibleSpace - 可以让UIBarButtonItem靠右显示
UIBarButtonSystemItemFixedSpace
UIBarButtonSystemItemCompose -
UIBarButtonSystemItemReply -
UIBarButtonSystemItemAction -
UIBarButtonSystemItemOrganize -
UIBarButtonSystemItemBookmarks -
UIBarButtonSystemItemSearch -
UIBarButtonSystemItemRefresh -
UIBarButtonSystemItemStop -
UIBarButtonSystemItemCamera -
UIBarButtonSystemItemTrash -
UIBarButtonSystemItemPlay -
UIBarButtonSystemItemPause -
UIBarButtonSystemItemRewind -
UIBarButtonSystemItemFastForward -
UIBarButtonSystemItemUndo -
UIBarButtonSystemItemRedo -
通过[self setToolBarItems:@[items1,items。。。]]添加到ToolBar上面
2.在键盘上方添加自定义按钮
UIToolbar * topView = [[UIToolbaralloc]initWithFrame:CGRectMake(0,0,[UIScreenmainScreen].bounds.size.width,30)];
[topView setBarStyle:UIBarStyleDefault];
UIBarButtonItem * btnSpace = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(4,5,40,25);
[btn setTitleColor:[UIColorblueColor]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(dismissKeyBoard)forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"完成"forState:UIControlStateNormal];
UIBarButtonItem *doneBtn = [[UIBarButtonItemalloc]initWithCustomView:btn];
NSArray * buttonsArray = [NSArrayarrayWithObjects:btnSpace,doneBtn,nil];
[topView setItems:buttonsArray];
[self.textFiled setInputAccessoryView:topView];//添加到键盘上方
-(void)dismissKeyBoard
{
[self.viewendEditing:YES];
}
运行效果如图:
3.UIBarButtonSystemItemFixedSpace的使用
UIBarButtonSystemItemFlexibleSpace能自动调节按钮间的间距;另外,不仅可以调整按钮间的间距,将其配置到左端(传递给setToolbarItems:方法的数组的第一个元素)时,可创建靠右的工具条按钮。同时配置到左右端(数组的第一项及最后一项)时,将创建居中的工具条按钮!
相关的文章链接: http://book.2cto.com/201209/4185.html, http://www.jianshu.com/p/532d12375e9a