刚好做到这个功能,记录一下自己走的坑.
方法一:
文章一
思路大概:
在UITextField或者UITextView里,有个'inputAccessoryView'属性
在这个属性上添加一个view,View里面有个UITextView或UITextField。
点击btn的时候,让View里面的UITextField或者UITextView成为第一响应者
这样就可以弹出键盘
方法二:
通过监听键盘弹出的通知进行相应的处理.
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:@"发布" 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 = @"请输入评论";
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];
设置这个View的Y值是屏幕高度,这样的话,就可以先达到把这个View隐藏起来的目的.然后点击需要唤起键盘的按钮,让这个View里面的输入框子控件,成为第一响应者.通过监听键盘弹出和收起的两个通知,做相应的动画处理(代码如下)
- (void)keyBoardWillShow:(NSNotification *)notification
{
// 获取用户信息
NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notification.userInfo];
// 获取键盘高度
CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyBoardHeight = keyBoardBounds.size.height;
// 获取键盘动画时间
CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 定义好动作
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];
// 获取键盘动画时间
CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 定义好动作
void (^animation)(void) = ^void(void) {
self.commentView.transform = CGAffineTransformIdentity;
};
if (animationTime > 0) {
[UIView animateWithDuration:animationTime animations:animation];
} else {
animation();
}}
注意:
UITextField单行文本输入框,不能多行.
如果把UITextField换成UITextView,如果没有对textView做内容自适应处理,多行的TextView,会被键盘遮挡.
让这个View里面的输入框子控件,成为第一响应者.
方法三
根据前面两个方法的思路,我把他们抽出来,封装一个类
import <UIKit/UIKit.h>
typedef void (^XBTextViewBlcok)(NSString text);
@interface XJWTextView : UIView
/ 唤醒 */
@property (nonatomic,strong) UITextView textView;
/ 发送文本 /
@property (nonatomic,copy) XBTextViewBlcok TextBlcok;
/ 设置占位符 */
-(void)setPlaceholderText:(NSString *)text;
@end
由于我懒了...我就直接上图片好了....
在控制器中:
效果图:
如果有更好的方法,欢迎指出.
大佬勿喷~