您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 控件入門:UIDatePicker 與 UIToolbar 的搭配用法

編輯:iOS 相關教學

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

一、UIDatePicker 控件

它自帶默認寬高,不需要設置數據源和代理,屬於开箱即用的控件。幾個要點:

  • 本地化:先確認系統語言,再通過 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 詳解

UIToolbar 只能容納 UIBarButtonItem 類型的子控件,其他控件都必須先被包裝成這種 item 才能放進去。item 會按照數組中的順序依次排列,中間可以插入可伸縮空白項做間隔。除了做鍵盤附件,它也常被用來製作自定義的導航或標題欄。

UIBarButtonItem 的四種初始化方式

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

通過 initWithCustomView 放入任意控件

這是最有意思的用法,簽名很簡單:

- (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:,工具欄上能放什麼就不再受限了。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved