iOS中鍵盤(pán) KeyBoard 上添加工具欄的方法
iOS中 鍵盤(pán) KeyBoard 上怎么添加工具欄?
如圖中所示 在鍵盤(pán)上面加一條工具欄
大致思路是提前創(chuàng)建好工具欄,在鍵盤(pán)彈出的時(shí)候?qū)⒐ぞ邫陲@示出來(lái),在鍵盤(pán)消失的時(shí)候讓工具欄隱藏
上代碼
設(shè)置兩個(gè)變量
UIView * _toolView; //工具欄 UITextField *textField;// 輸入框 呼出鍵盤(pán)用
創(chuàng)建工具欄 輸入框 添加鍵盤(pán)彈出 消失的通知
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 64, 120, 60)]; textField.placeholder = @"測(cè)試"; [self.view addSubview:textField]; //增加監(jiān)聽(tīng),當(dāng)鍵盤(pán)出現(xiàn)或改變時(shí)收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加監(jiān)聽(tīng),當(dāng)鍵退出時(shí)收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; //初始化工具欄 _toolView = [[UIView alloc]init]; _toolView.frame = CGRectMake(0, screen_Height, screen_Width, 50); [self.view addSubview:_toolView]; UIButton *losebtn = [UIButton buttonWithType:UIButtonTypeCustom]; losebtn.frame = CGRectMake(20, 0, 50, 50); [losebtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [losebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [losebtn setTitle:@"收起" forState:UIControlStateNormal]; [_toolView addSubview:losebtn]; UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [imageBtn setTitle:@"圖片" forState:UIControlStateNormal]; imageBtn.frame = CGRectMake(screen_Width-100, 0, 50, 50); [imageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [imageBtn addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_toolView addSubview:imageBtn]; UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [cameraBtn setTitle:@"相機(jī)" forState:UIControlStateNormal]; cameraBtn.frame = CGRectMake(screen_Width-50, 0, 50, 50); [cameraBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [cameraBtn addTarget:self action:@selector(cameraBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_toolView addSubview:cameraBtn]; UIButton *canclebtn = [UIButton buttonWithType:UIButtonTypeCustom]; [canclebtn setTitle:@"取消" forState:UIControlStateNormal]; canclebtn.frame = CGRectMake(screen_Width-150, 0, 50, 50); [canclebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [canclebtn addTarget:self action:@selector(canclebtnBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_toolView addSubview:canclebtn]; }
實(shí)現(xiàn)鍵盤(pán)通知的方法
#pragma mark 當(dāng)鍵盤(pán)出現(xiàn)或改變時(shí)調(diào)用 - (void)keyboardWillShow:(NSNotification *)aNotification { //鍵盤(pán)彈出時(shí)顯示工具欄 //獲取鍵盤(pán)的高度 NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; float keyBoardHeight = keyboardRect.size.height; // NSLog(@"%ld",(long)keyBoardHeight); [UIView animateWithDuration:0.1 animations:^{ _toolView.frame = CGRectMake(0, screen_Height-keyBoardHeight-50, screen_Width, 50); }]; } #pragma mark 當(dāng)鍵退出時(shí)調(diào)用 - (void)keyboardWillHide:(NSNotification *)aNotification { //鍵盤(pán)消失時(shí) 隱藏工具欄 [UIView animateWithDuration:0.1 animations:^{ _toolView.frame = CGRectMake(0, screen_Height+50, screen_Width, 50); }]; }
給工具欄上的各個(gè)按鈕實(shí)現(xiàn)點(diǎn)擊事件
- (void)btnClick{ [textField resignFirstResponder]; } - (void)imageBtnClick{ } - (void)cameraBtnClick{ } - (void)canclebtnBtnClick{ }
PS:下面看下iOS 鍵盤(pán)上方增加工具欄的代碼。
具體代碼如下所示:
UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init]; [keyboardDoneButtonView sizeToFit]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)]; [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; txtField.inputAccessoryView = keyboardDoneButtonView;
- iOS應(yīng)用開(kāi)發(fā)中監(jiān)聽(tīng)鍵盤(pán)事件的代碼實(shí)例小結(jié)
- iOS中的UIKeyboard鍵盤(pán)視圖使用方法小結(jié)
- iOS實(shí)現(xiàn)輸入框跟隨鍵盤(pán)自動(dòng)上移的實(shí)例代碼
- iOS項(xiàng)目開(kāi)發(fā)鍵盤(pán)彈出遮擋輸入框問(wèn)題解決方案
- h5 ios輸入框和鍵盤(pán)的兼容性優(yōu)化指南
- iOS開(kāi)發(fā)第三方鍵盤(pán)處理實(shí)例代碼
- iOS自定義鍵盤(pán)切換效果
- IOS數(shù)字鍵盤(pán)左下角添加完成按鈕的實(shí)現(xiàn)方法
- iOS中只讓textField使用鍵盤(pán)通知的實(shí)例代碼
- iOS自定義身份證鍵盤(pán)
相關(guān)文章
iOS手勢(shì)識(shí)別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢(shì)依賴,自定義手勢(shì))
這篇文章主要介紹了iOS手勢(shì)識(shí)別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢(shì)依賴,自定義手勢(shì)),具有一定的參考價(jià)值,有需要的可以參考一下。2016-11-11查看iOS已上架App的Crash信息定位、應(yīng)對(duì)處理方式的實(shí)例
下面小編就為大家?guī)?lái)一篇查看iOS已上架App的Crash信息定位、應(yīng)對(duì)處理方式的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12iOS中實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪圖片功能實(shí)例
圖片處理中經(jīng)常用的圖片剪裁,就是通過(guò)剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,下面這篇文章主要給大家介紹了關(guān)于iOS中實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪圖片功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起看看吧。2017-11-11iOS開(kāi)發(fā)微信收款到賬語(yǔ)音提醒功能思路詳解
這篇文章主要介紹了iOS開(kāi)發(fā)微信收款到賬語(yǔ)音提醒功能思路詳解,需要的朋友可以參考下2017-09-09iOS開(kāi)發(fā)中不合法的網(wǎng)絡(luò)請(qǐng)求地址如何解決
這篇文章主要介紹了iOS開(kāi)發(fā)中不合法的網(wǎng)絡(luò)請(qǐng)求地址的解決方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能
這篇文章主要為大家詳細(xì)介紹了iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06