ios開發-inputView和inputAccessoryView

來源:互聯網
上載者:User

標籤:ios開發   inputview   

說明

在UITextField和UITextField中能查到這兩個屬性

@property (readwrite, retain) UIView *inputView;

@property (readwrite, retain) UIView *inputAccessoryView;

在UITextField或者UITextField成為預設響應的時候,會彈出系統鍵盤。如果對這兩個控制項的inputView屬性設定了自訂的view,在其成為第一響應的時候系統鍵盤將不再彈出,取而代之的是賦值給inputView的那個view。inputAccessoryView是鍵盤的輔助視圖,即鍵盤上面那部分。同樣當對inputAccessoryView設定了自訂view時,鍵盤彈出的同時,該view會作為輔助視圖出現在鍵盤的上面,和鍵盤一起彈出。通常會使用pickerView作為自訂的彈出視圖,這裡為了簡單起見,用一個普通的view示範:

基礎代碼示範
#import "ViewController.h"#define SCREEN_BOUNDS [UIScreen mainScreen].bounds@interface ViewController ()@property (nonatomic, strong) UITextField *textField;@property (nonatomic, strong) UIView *customInputView;  @property (nonatomic, strong) UIToolbar *customAccessoryView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    [self loadBaseUI];}- (void)loadBaseUI{    [self.view addSubview:self.textField];}- (UITextField *)textField{    if (!_textField) {        _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, SCREEN_BOUNDS.size.width - 100, 30)];        _textField.layer.borderWidth = 1.0;        _textField.layer.borderColor = [UIColor lightGrayColor].CGColor;        _textField.layer.cornerRadius = 4.0;        _textField.placeholder = @"測試";        _textField.inputView = self.customInputView;        _textField.inputAccessoryView = self.customAccessoryView;    }    return _textField;}- (UIView *)customInputView{    if (!_customInputView) {        _customInputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, 220)];        _customInputView.backgroundColor = [UIColor lightGrayColor];        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_BOUNDS.size.width, 40)];        label.textAlignment = NSTextAlignmentCenter;        label.text = @"自訂inputView";        [_customInputView addSubview:label];    }    return _customInputView;}- (UIToolbar *)customAccessoryView{    if (!_customAccessoryView) {        _customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCREEN_BOUNDS.size.width,40}];        _customAccessoryView.barTintColor = [UIColor orangeColor];        UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];        UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];        [_customAccessoryView setItems:@[space,space,finish]];    }    return _customAccessoryView;}- (void)done{    [self.textField resignFirstResponder];}@end
點擊textField後的顯示效果

對其他控制項設定inputView和inputAccessoryView

除了UITextField和UITextField,有事我們可能想讓別的控制項實作類別似效果,比如點擊一個按鈕、選中某個cell的時候彈出鍵盤,但是對於UITextField和UITextField以外的控制項,inputView和inputAccessoryView是唯讀

@property (nonatomic, readonly, retain) UIView *inputView

@property (nonatomic, readonly, retain) UIView *inputAccessoryView

因此如果要使用控制項這兩個屬性,就要建立該控制項的子視圖,然後重新申明inputView和inputAccessoryView為readwrite的,並重寫它們的get方法,這樣在UITableViewCell成為第一響應的時候會自動彈出inputView和inputAccessoryView。以UITableView示範:

程式碼範例
//MyTableViewCell.h#import <UIKit/UIKit.h>#define SCREEN_BOUNDS [UIScreen mainScreen].bounds@interface MyTableViewCell : UITableViewCell//屬性申明為可讀寫@property (nonatomic, strong, readwrite) UIView *inputView;@property (nonatomic, strong, readwrite) UIToolbar *inputAccessoryView;@end
//  MyTableViewCell.m#import "MyTableViewCell.h"@implementation MyTableViewCell//重寫get方法- (UIView *)inputView{    if (!_inputView) {        _inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, 220)];        _inputView.backgroundColor = [UIColor lightGrayColor];        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_BOUNDS.size.width, 40)];        label.textAlignment = NSTextAlignmentCenter;        label.text = @"自訂inputView";        [_inputView addSubview:label];    }    return _inputView;}- (UIToolbar *)inputAccessoryView{    if (!_inputAccessoryView) {        _inputAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCREEN_BOUNDS.size.width,40}];        _inputAccessoryView.barTintColor = [UIColor orangeColor];        UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];        UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];        [_inputAccessoryView setItems:@[space,space,finish]];    }    return _inputAccessoryView;}- (void)done{    [self resignFirstResponder];}@end

在主視圖控制器中建立一個tableView發現這樣之後點擊cell發現不會彈出inputView,最後查閱官方文檔發現需要在子類中重寫canBecomeFirstResponder方法:
在子類.m檔案中添加

- (BOOL)canBecomeFirstResponder{    return YES;}

發現還是不行,最後手動becomeFirstResponder,終於實現了。PS原來選中都還不算是成為第一響應,坑爹啊!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    MyTableViewCell *cell = (MyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];    [cell becomeFirstResponder];}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

ios開發-inputView和inputAccessoryView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.