国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS鍵盤如何添加隱藏鍵盤功能

2020-07-26 01:29:37
字體:
供稿:網(wǎng)友

本文實例為大家分享了iOS添加隱藏鍵盤功能的具體方法,供大家參考,具體內(nèi)容如下

鍵盤添加個隱藏鍵盤功能

使用方法:導(dǎo)入XMCustomKeyBoard.h
[XMCustomKeyBoard CancelableKeyboard:控件對象 ];
控件對象可以是UITextFiled,UITextView,UISearchBar 等一系列調(diào)用鍵盤輸入的類的實例

1.自定義個UIBarButtonItem,添加屬性editableView,editableView存儲需要添加隱藏鍵盤功能的那個控件

#import <UIKit/UIKit.h>@interface XMCustomKeyBoardBtn : UIBarButtonItem@property (strong, nonatomic) id editableView;@end
#import "XMCustomKeyBoardBtn.h"@implementation XMCustomKeyBoardBtn@end

2.自定義個UIView,因為只有UIView的子類才可以添加進(jìn)keyWindow,想動態(tài)綁定這個類定義的方法,就必須讓這個類保持活躍。

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>#import "XMCustomKeyBoardBtn.h"@interface XMCustomKeyBoard : UIView+ (void) CancelableKeyboard:(id) editableView;+ (void) CancelableKeyboard:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn;@end

3.通過傳進(jìn)來的控件為其在鍵盤工具欄添加一個隱藏鍵盤的按鈕并動態(tài)綁定一個隱藏鍵盤的方法

#import "XMCustomKeyBoard.h"@implementation XMCustomKeyBoard+ (void) CancelableKeyboard:(id) editableView{  XMCustomKeyBoard *custom = [[XMCustomKeyBoard alloc] initWithFrame:CGRectMake(0,-999,10,10)];  [[UIApplication sharedApplication].keyWindow addSubview:custom];  [editableView setInputAccessoryView:[self CancelableKeyboardToolBar:editableView addTarget:custom]];}+ (void) CancelableKeyboard:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn {  XMCustomKeyBoard *custom = [[XMCustomKeyBoard alloc] initWithFrame:CGRectMake(0,-10,10,10)];  [[UIApplication sharedApplication].keyWindow addSubview:custom];  [editableView setInputAccessoryView:[self CancelableKeyboardToolBar:editableView CustomButtonItem:btn addTarget:custom]];}+ (UIToolbar *)CancelableKeyboardToolBar:(id) editableView CustomButtonItem:(UIBarButtonItem *)btn addTarget:(id) target{  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIApplication sharedApplication].keyWindow.frame), 40)];  toolbar.backgroundColor = [UIColor lightGrayColor];    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:editableView action:@selector(onClick)];  [button setWidth:[UIApplication sharedApplication].keyWindow.frame.size.width - btn.width];  XMCustomKeyBoardBtn *button1 = (XMCustomKeyBoardBtn *)btn;    button1.target = target;    button1.action = @selector(CancelableKeyboard:);    button1.editableView = editableView;           [toolbar setItems:@[button,button1]];  return toolbar;}+ (UIToolbar *)CancelableKeyboardToolBar:(id) editableView addTarget:(id) target{  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIApplication sharedApplication].keyWindow.frame), 40)];  toolbar.backgroundColor = [UIColor lightGrayColor];    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:editableView action:@selector(onClick)];  [button setWidth:[UIApplication sharedApplication].keyWindow.frame.size.width - 50];  XMCustomKeyBoardBtn *button1 = [[XMCustomKeyBoardBtn alloc] initWithTitle:@"隱藏鍵盤" style:UIBarButtonItemStyleBordered target:target action:@selector(CancelableKeyboard:)];    button1.editableView = editableView;           [button1 setWidth:50];  [toolbar setItems:@[button,button1]];  return toolbar;}-(void)CancelableKeyboard:(XMCustomKeyBoardBtn *) btn{  [btn.editableView resignFirstResponder];}-(void) onClick{  }@end

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。/p>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴隆县| 修武县| 沁源县| 安宁市| 永清县| 罗源县| 岳阳县| 同仁县| 武强县| 鹰潭市| 景宁| 岗巴县| 大同县| 江城| 葵青区| 平定县| 蓝山县| 甘南县| 同仁县| 乐昌市| 汾阳市| 湘潭县| 灵武市| 绥宁县| 湖南省| 安岳县| 乌海市| 根河市| 奎屯市| 乳源| 大邑县| 桐柏县| 朔州市| 邵阳县| 双鸭山市| 吉木乃县| 莱芜市| 资源县| 郑州市| 滦南县| 维西|