做表單類界面時,日期選擇器和工具欄幾乎是標配組合。本文把 UIDatePicker 與 UIToolbar 的常用配置整理成一份可直接參照的速查筆記。

它自帶默認寬高,不需要設置數據源和代理,屬於开箱即用的控件。幾個要點:
locale 屬性指定地區,例如 zh-CN,界面就會顯示中文。datePickerMode 提供時間與日期的四種組合模式,需要時也可以選 UIDatePickerModeCustom 自定義。minimumDate 與 maximumDate,超出範圍會自動回弹到最近的邊界值。#import "YYViewController.h"
@interface YYViewController ()
/**
* 文本輸入框
*/
@property (strong, nonatomic) IBOutlet UITextField *textfield;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1. 創建日期選擇器
UIDatePicker *date = [[UIDatePicker alloc] init];
// 設置為中文
[date setLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
// 只顯示日期
date.datePickerMode = UIDatePickerModeDate;
// 光標聚焦到輸入框時,彈出日期選擇器
self.textfield.inputView = date;
// 2. 創建工具欄
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.barTintColor = [UIColor brownColor];
toolbar.frame = CGRectMake(0, 0, 320, 44);
UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithTitle:@"上一個"
style:UIBarButtonItemStylePlain
target:self
action:@selector(click)];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"下一個"
style:UIBarButtonItemStylePlain
target:self
action:@selector(click)];
// 可伸縮的空白項,用來把「完成」推到最右側
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"完成"
style:UIBarButtonItemStylePlain
target:self
action:@selector(click)];
toolbar.items = @[item0, item1, item2, item3];
// 作為輸入框鍵盤的附件視圖
self.textfield.inputAccessoryView = toolbar;
}
- (void)click
{
NSLog(@"toolbar");
}
@end

UIToolbar 只能容納 UIBarButtonItem 類型的子控件,其他控件都必須先被包裝成這種 item 才能放進去。item 會按照數組中的順序依次排列,中間可以插入可伸縮空白項做間隔。除了做鍵盤附件,它也常被用來製作自定義的導航或標題欄。
- initWithTitle:——最常用,用文字創建按鈕。- initWithImage:——用圖片創建按鈕。- initWithBarButtonSystemItem:——使用系統預置樣式,形狀和尺寸都已固定,Apple 官方文檔裡有完整的樣式列表。- initWithCustomView:——最靈活的一種,可以塞入任意 UIView。UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
NSMutableArray *myToolBarItems = [NSMutableArray array];
[myToolBarItems addObject:[[[UIBarButtonItem alloc]
initWithTitle:@"myTitle"
style:UIBarButtonItemStylePlain
target:self
action:@selector(action)] autorelease]];
[myToolBar setItems:myToolBarItems animated:YES];
[myToolBar release];
setItems: 接收的是一個對象數組。
[myToolBarItems addObject:[[[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"myImage.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(action)] autorelease]];
[myToolBarItems addObject:[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(action)] autorelease]];
系統樣式的枚舉定義如下,常用的有 Done、Cancel、Edit、Save、Add、Search、Refresh、Camera、Play、Pause 等:
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem
target:(id)target
action:(SEL)action;
typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo, // iPhoneOS 3.0
UIBarButtonSystemItemRedo, // iPhoneOS 3.0
} UIBarButtonSystemItem;
這是最有意思的用法,簽名很簡單:
- (id)initWithCustomView:(UIView *)customView
參數是一個 UIView,所以任何能放到視圖層級上的東西都可以進工具欄。
A. 放一個開關:
[myToolBarItems addObject:[[[UIBarButtonItem alloc]
initWithCustomView:[[[UISwitch alloc] init] autorelease]]
autorelease]];
B. 放一個普通按鈕並調整寬度:
UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]
initWithTitle:@"myButton"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(action)] autorelease];
myButton.width = 50;
[myToolBarItems addObject:myButton];
C. 放一個文本標籤:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)]; myLabel.font = [UIFont systemFontOfSize:10]; UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myLabel]; [myToolBarItems addObject:myButtonItem]; [myLabel release]; [myButtonItem release];
D. 放一個進度條:
UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)]; UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myProgress]; [myToolBarItems addObject:myButtonItem]; [myProgress release]; [myButtonItem release];
掌握了 initWithCustomView:,工具欄上能放什麼就不再受限了。