剛好做到這個(gè)功能,記錄一下自己走的坑.
方法一:
文章一
思路大概:
在UITextField或者UITextView里,有個(gè)'inputAccessoryView'屬性
在這個(gè)屬性上添加一個(gè)view,View里面有個(gè)UITextView或UITextField。
點(diǎn)擊btn的時(shí)候,讓View里面的UITextField或者UITextView成為第一響應(yīng)者
這樣就可以彈出鍵盤
方法二:
通過(guò)監(jiān)聽(tīng)鍵盤彈出的通知進(jìn)行相應(yīng)的處理.
UIView *keyBoardTopView = [[UIView alloc] initWithFrame:CGRectMake(0, XJWScreenH, XJWScreenW, 50)];
keyBoardTopView.backgroundColor = [UIColor whiteColor];
keyBoardTopView.layer.borderWidth = 0.7;
keyBoardTopView.layer.borderColor = [UIColor orangeColor].CGColor;
self.sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(keyBoardTopView.bounds.size.width-60-12, 4, 60, 45)];
self.sendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[self.sendBtn setTitle:@"發(fā)布" forState:UIControlStateNormal];
self.sendBtn.alpha = 0.4;
self.sendBtn.tag = 2002;
[self.sendBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.sendBtn addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside];
[keyBoardTopView addSubview:self.sendBtn];
UITextField *inputTF = [[UITextField alloc] init];
inputTF.frame = CGRectMake(10, 4, XJWScreenW - 10 - 72, 42);
inputTF.placeholder = @"請(qǐng)輸入評(píng)論";
inputTF.tag = 1001;
inputTF.layer.cornerRadius = 5;
inputTF.layer.masksToBounds = YES;
self.commentTF = inputTF;
inputTF.backgroundColor = [UIColor redColor];
[inputTF addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
[keyBoardTopView addSubview:inputTF];
self.commentView = keyBoardTopView;
[self.view addSubview:keyBoardTopView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
設(shè)置這個(gè)View的Y值是屏幕高度,這樣的話,就可以先達(dá)到把這個(gè)View隱藏起來(lái)的目的.然后點(diǎn)擊需要喚起鍵盤的按鈕,讓這個(gè)View里面的輸入框子控件,成為第一響應(yīng)者.通過(guò)監(jiān)聽(tīng)鍵盤彈出和收起的兩個(gè)通知,做相應(yīng)的動(dòng)畫處理(代碼如下)
- (void)keyBoardWillShow:(NSNotification *)notification
{
// 獲取用戶信息
NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notification.userInfo];
// 獲取鍵盤高度
CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyBoardHeight = keyBoardBounds.size.height;
// 獲取鍵盤動(dòng)畫時(shí)間
CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 定義好動(dòng)作
void (^animation)(void) = ^void(void) {
self.commentView.transform = CGAffineTransformMakeTranslation(0, -(keyBoardHeight + self.textView.bounds.size.height));
};
if (animationTime > 0) {
[UIView animateWithDuration:animationTime animations:animation];
} else {
animation();
}
}
- (void)keyBoardWillHide:(NSNotification *)notificaiton
{
// 獲取用戶信息
NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notificaiton.userInfo];
// 獲取鍵盤動(dòng)畫時(shí)間
CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 定義好動(dòng)作
void (^animation)(void) = ^void(void) {
self.commentView.transform = CGAffineTransformIdentity;
};
if (animationTime > 0) {
[UIView animateWithDuration:animationTime animations:animation];
} else {
animation();
}}
注意:
UITextField單行文本輸入框,不能多行.
如果把UITextField換成UITextView,如果沒(méi)有對(duì)textView做內(nèi)容自適應(yīng)處理,多行的TextView,會(huì)被鍵盤遮擋.
讓這個(gè)View里面的輸入框子控件,成為第一響應(yīng)者.
方法三
根據(jù)前面兩個(gè)方法的思路,我把他們抽出來(lái),封裝一個(gè)類
import <UIKit/UIKit.h>
typedef void (^XBTextViewBlcok)(NSString text);
@interface XJWTextView : UIView
/ 喚醒 */
@property (nonatomic,strong) UITextView textView;
/ 發(fā)送文本 /
@property (nonatomic,copy) XBTextViewBlcok TextBlcok;
/ 設(shè)置占位符 */
-(void)setPlaceholderText:(NSString *)text;
@end
由于我懶了...我就直接上圖片好了....











在控制器中:

效果圖:

如果有更好的方法,歡迎指出.
大佬勿噴~