
有花堪折直須折,莫到無花空折枝!<轟隆雉雞>
效果圖:
自定義發(fā)送框適應(yīng)鍵盤彈出

自定義發(fā)送框適應(yīng)鍵盤彈出
分析:
正常情況下底部發(fā)送的發(fā)送框固定好位置后, 鍵盤彈出會(huì)把它擋住!那么就需要我們?cè)阪I盤彈出的時(shí)候改變我們發(fā)送框工具欄的位置;
思路:
首先: 整體上來看 textView 和 "聲音" "表情" "加號(hào)" 三個(gè) Button 添加到一個(gè) View上,然后對(duì) View 進(jìn)行約束, 這里需要其左邊距離(父視圖View)0,右邊距0, 下面0 然后固定一個(gè)高度
其次: 監(jiān)聽鍵盤的行為當(dāng)鍵盤彈出來的時(shí)候, 我們把發(fā)送框所在的 View 底部的約束進(jìn)行適當(dāng)?shù)男薷?讓其顯示在鍵盤的上方, 鍵盤消失后回到原來位置
代碼實(shí)現(xiàn):
1: 把發(fā)送框所在 View 的距離下部的約束拉進(jìn)響應(yīng)控制器作為屬性
// 工具條底部約束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolBottomConstraint;
2: 使用系統(tǒng)通話實(shí)現(xiàn)監(jiān)聽鍵盤的行為
#pragma mark 通知監(jiān)聽鍵盤彈出情況 使用的是系統(tǒng)的通知
#當(dāng)鍵盤彈出的時(shí)候 執(zhí)行kbWillShow:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillShow:) name:UIKeyboardWillShowNotification object:nil];
#當(dāng)鍵盤消失的時(shí)候 執(zhí)行kbHideShow:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbHideShow:) name:UIKeyboardWillHideNotification object:nil];```
3: 實(shí)現(xiàn)響應(yīng)通知的方法
\#pragma mark 鍵盤彈出 觸發(fā)事件
```code
# 鍵盤彈出 調(diào)整約束的高度
- (void)kbWillShow:(NSNotification *)noti
{
// 獲取鍵盤的高度 首先獲取當(dāng)前鍵盤的 Rect
CGRect kbFram = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat kbHeight = kbFram.size.height;
// 把約束改掉
self.toolBottomConstraint.constant = kbHeight ;
[UIView animateWithDuration:1 delay:0 options: UIViewAnimationOptionCurveEaseInOut animations:^{
// 布控子視圖
[self.view layoutIfNeeded];
} completion:nil];
}
# 鍵盤收起 一切回到夢(mèng)開始的地方
- (void)kbHideShow:(NSNotification *)noti
{
// 把約束改成開始的 0
self.toolBottomConstraint.constant = 0 ;
[UIView animateWithDuration:0.1 delay:0 options: UIViewAnimationOptionCurveEaseInOut animations:^{
// 布控子視圖
[self.view layoutIfNeeded];
} completion:nil];
}```
有時(shí)間在寫高度適應(yīng)的問題, 最近寫點(diǎn)小項(xiàng)目! 加油!