設(shè)置自定義鍵盤(pán)與系統(tǒng)鍵盤(pán)切換判斷邏輯:
composeTextView : 自定義的TextView(繼承自UITextView)
bottomToolBar : 自定義的輔助工具條(繼承自UIView)
控制器下切換鍵盤(pán):
// 切換鍵盤(pán)方法
func switchKeyboard () -> Void {
// 系統(tǒng)鍵盤(pán) 點(diǎn)擊切換按鈕后,切換到自定義鍵盤(pán)
if composeTextView.inputView == nil {
composeTextView.inputView = UIButton(type: UIButtonType.ContactAdd)
} else {
// 自定義鍵盤(pán) 點(diǎn)擊切換按鈕后,切換到系統(tǒng)鍵盤(pán)
composeTextView.inputView = nil
}
// 刷新inputView
composeTextView.reloadInputViews()
// 成為第一響應(yīng)者
composeTextView.becomeFirstResponder()
}
自定義鍵盤(pán)(button演示):
keyboard_01.png
系統(tǒng)鍵盤(pán):
keyboard_02.png
自定義鍵盤(pán)ToolBar按鈕圖片切換:
在bottomToolBar類(lèi)中聲明一個(gè)Bool類(lèi)型的對(duì)外屬性,用來(lái)作為按鈕狀態(tài)切換的標(biāo)識(shí)
如果isEmoticon == true 代表當(dāng)前為自定義鍵盤(pán) button 顯示image為鍵盤(pán)
如果isEmoticon == false 代表當(dāng)前為系統(tǒng)鍵盤(pán) button 顯示image為自定義鍵盤(pán)
代碼:
// 表情鍵盤(pán)按鈕標(biāo)識(shí) (供外界設(shè)置)
/*
如果isEmoticon == true 代表當(dāng)前為自定義鍵盤(pán) button顯示image為系統(tǒng)鍵盤(pán)
如果isEmoticon == false 代表當(dāng)前為系統(tǒng)鍵盤(pán) button顯示image為自定義鍵盤(pán)
*/
var isEmoticon: Bool = false{
didSet{
if isEmoticon {
//圖標(biāo)顯示系統(tǒng)鍵盤(pán)(當(dāng)前是自定義鍵盤(pán))
emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
} else {
//圖標(biāo)顯示自定義鍵盤(pán)(當(dāng)前是系統(tǒng)鍵盤(pán))
emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
}
}
}
// 表情鍵盤(pán)按鈕 因?yàn)樾枰獙?duì)齊進(jìn)行設(shè)置,所以需要抽取出來(lái)
var emoticonButton: UIButton?
在控制器中點(diǎn)擊自定義ToolBar內(nèi)按鈕切換鍵盤(pán)時(shí),設(shè)置標(biāo)識(shí)(通過(guò)閉包監(jiān)聽(tīng)Button)
// 切換鍵盤(pán)
func switchKeyboard () -> Void {
// 系統(tǒng)鍵盤(pán) 點(diǎn)擊切換按鈕后,切換到自定義鍵盤(pán)
if composeTextView.inputView == nil {
composeTextView.inputView = keyboard
bottomToolBar.isEmoticon = true
} else {
// 自定義鍵盤(pán) 點(diǎn)擊切換按鈕后,切換到系統(tǒng)鍵盤(pán)
composeTextView.inputView = nil
bottomToolBar.isEmoticon = false
}
// 刷新inputView
composeTextView.reloadInputViews()
// 成為第一響應(yīng)者
composeTextView.becomeFirstResponder()
}
顯示自定義鍵盤(pán)時(shí),圖標(biāo)顯示為系統(tǒng)鍵盤(pán)
emo.png
顯示系統(tǒng)鍵盤(pán)時(shí),圖標(biāo)顯示為自定義鍵盤(pán)
key.png