最近在寫一個(gè)小應(yīng)用洪添,有一個(gè)評(píng)論功能垦页,類似微信的輸入,那么就希望在彈出鍵盤時(shí)干奢,輸入框自動(dòng)往上移動(dòng)到鍵盤上方痊焊。當(dāng)輸入內(nèi)容超過(guò)一行時(shí),輸入框高度自動(dòng)調(diào)整忿峻,但當(dāng)達(dá)到一定高度時(shí)薄啥,以滾動(dòng)條的方式滾動(dòng)內(nèi)容。
效果如圖
先說(shuō)說(shuō)控件逛尚,底部是一個(gè)UIView垄惧,然后UIView內(nèi)部加入一個(gè)UITextView和UIButton。布局用Autolayout黑低。使UIView距離左赘艳,右,低邊距為0克握,高度為64蕾管。UIView內(nèi)部的UITextView距離上下左右邊距為8,UIButton距離上右邊距為8菩暗,高度為30掰曾,寬度為50.
添加兩個(gè)NSLayoutConstraint,關(guān)聯(lián)為UIView的低邊距和高度
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *inputViewHeight;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *inputViewButtom;
當(dāng)彈出鍵盤時(shí)停团,輸入框自動(dòng)往上移動(dòng)到鍵盤上方旷坦,其原理非常簡(jiǎn)單,就是把inputViewButtom的低邊距修改為鍵盤的高度佑稠,這樣UIView就自然調(diào)整到鍵盤的上方了秒梅。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 修改工具條底部約束
_inputViewButtom.constant = MainScreenHeight - keyboardFrame.origin.y;
// 獲得鍵盤彈出或隱藏時(shí)間
CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 添加動(dòng)畫
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardWillHidden:(NSNotification *)notification {
_inputViewButtom.constant = 0.0f;
}
當(dāng)輸入內(nèi)容超過(guò)一行時(shí),輸入框高度自動(dòng)調(diào)整舌胶,但當(dāng)達(dá)到一定高度時(shí)捆蜀,以滾動(dòng)條的方式滾動(dòng)內(nèi)容。其原理非常簡(jiǎn)單幔嫂,就是根據(jù)輸入的內(nèi)容調(diào)整UIView的高度辆它,由于UITextView距離上下左右邊距為8,當(dāng)UIView的高度變化時(shí)履恩,UITextView的高度也會(huì)變化锰茉。
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
[self sendComment];
[_inputTextView resignFirstResponder];
return NO;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
[self updateInputViewUI];
}
- (void)updateInputViewUI {
static CGFloat minHeight = 30.0f;
static CGFloat maxHeight = 80.0f;
CGSize constraintSize = CGSizeMake(MainScreenWidth - 78.0f, MAXFLOAT);
CGSize size = [_inputTextView sizeThatFits:constraintSize];
if (size.height <= minHeight) {
size.height = minHeight;
} else {
if (size.height >= maxHeight) {
size.height = maxHeight;
_inputTextView.scrollEnabled = YES; // 允許滾動(dòng)
} else {
_inputTextView.scrollEnabled = NO; // 不允許滾動(dòng)
}
}
_inputTextView.frame = CGRectMake(10.0f, 8.0f, MainScreenWidth - 78.0f, size.height);
_inputViewHeight.constant = size.height + 16.0f;
}
- (void)sendComment {
_inputTextView.text = @"";
[self updateInputViewUI];
//
}
- (IBAction)clickSendButton:(id)sender {
[_inputTextView resignFirstResponder];
if ([_inputTextView.text isEqualToString:@""]) {
//
} else {
[self sendComment];
}
}
點(diǎn)擊Send或者發(fā)送按鈕,最后調(diào)整為初始狀態(tài)切心。
代碼基本都列清楚了飒筑,大家也可以參考github里的工程,可以運(yùn)行來(lái)看看绽昏。https://github.com/Inspirelife96/ILSamples/tree/master/InputField