標籤: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