ios中自定義鍵盤+回收

ios中自定義鍵盤+回收

//? 自定義鍵盤

UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 256)];

field.inputView = keyView;

// 1、添加一個按鈕

UIButton *keyBtn = [UIButton buttonWithType:UIButtonTypeSystem];

keyBtn.frame = CGRectMake(0, 0, 50, 40);

[keyBtn addTarget:self action:@selector(haha:)? ? ? ? ? ? forControlEvents:UIControlEventTouchUpInside];

[keyBtn setTitle:@"1" forState:UIControlStateNormal];

keyBtn.backgroundColor = [UIColor whiteColor];

[keyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[keyView addSubview:keyBtn];

// 1.1點擊按鈕,實現(xiàn)可以讓按鈕的標題顯示在輸入框中

- (void)haha: (UIButton *)sender{

//? 得到輸入框

UITextField? *fiel = (UITextField *)[self.window viewWithTag:2000];

// 得到按鈕標題,因為標題是個字符串,所以得到的結(jié)果得讓字符串承接

NSString *titel = [sender titleForState:UIControlStateNormal];

//? ? 將標題顯示在輸入框中

//? ? fiel.text = titel;

//? ? 拼接字符,也可以使用這種拼接的方式,來連續(xù)輸入

fiel.text = [fiel.text stringByAppendingString:titel];

}

//2、 添加一個回收鍵盤的按鈕

初始化創(chuàng)建? ? UIButton *keyBtn1 = [UIButton buttonWithType:UIButtonTypeSystem];

設(shè)置大小 keyBtn1.frame = CGRectMake(100, 0, 50, 40);

添加關(guān)聯(lián)方法? [keyBtn1 addTarget:self action:@selector(huishou:) forControlEvents:UIControlEventTouchUpInside];

設(shè)置標題 [keyBtn1 setTitle:@"Return" forState:UIControlStateNormal];

設(shè)置背景色? keyBtn1.backgroundColor = [UIColor whiteColor];

設(shè)置標題顏色 [keyBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

呈現(xiàn)? [keyView addSubview:keyBtn1];

//回收鍵盤的按鈕實現(xiàn)方法

- (void)huishou:(UIButton *)sender{

得到textfield? UITextField? *fiel = (UITextField *)[self.window viewWithTag:2000];

//? 回收鍵盤,取消輸入框的第一響應(yīng)者,結(jié)束輸入框的編輯狀態(tài)

[fiel resignFirstResponder];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容