ios UITextField和UITextView 鍵盤添加“完成”按鈕

效果圖:

分別寫一個類繼承與UITextField和UITextView,現在以UITextField爲列,UITextView同理

CustomizeTextField.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CustomizeTextField : UITextField

@end

NS_ASSUME_NONNULL_END

CustomizeTextField.m

#import "CustomizeTextField.h"

@implementation CustomizeTextField

 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
     [self setDefaultInputAccessoryViewWithTarget:self action:@selector(numberFieldCancle)];
 }
 
- (void)setDefaultInputAccessoryViewWithTarget:(id)target action:(SEL) action{
    UIToolbar *toolBar =[UIToolbar new];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:target action:action];
    doneBtn.tintColor=[UIColor blueColor];
    NSArray*items =@[flexSpace,doneBtn];
    toolBar.items= items;
    [toolBar sizeToFit];
    self.inputAccessoryView = toolBar;
}

- (void)numberFieldCancle{
    [self resignFirstResponder];  //收起鍵盤
}
@end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章