弹出键盘怎样把tableview往上顶?不遮住输入行

弹出键盘怎样把tableview往上顶?不遮住输入行   
 
 
 
 

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
    NSLog(@"register");
}
- (void)keyboardWasShown:(NSNotification *)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    displayTable.scrollEnabled = YES;
    displayTable.contentInset = contentInsets;
    displayTable.scrollIndicatorInsets = contentInsets;
    NSLog(@"keyboardWasShown");
    
    // If active text field is hidden by keyboard, scroll it so it‘s visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.superview.superview.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.superview.superview.frame.origin.y-aRect.size.height+44);
        [displayTable setContentOffset:scrollPoint animated:YES];
    }    
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification {
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    displayTable.contentInset = contentInsets;
    displayTable.scrollIndicatorInsets = contentInsets;
}

[self registerForKeyboardNotifications]


 

做出来了,注册UIKeyboardDidShowNotification通知,在里面可以得到键盘的大小,算出键盘的搜索条间的矩形,加上一个背景为黑色 alpha为0.9的UIView 就行了,有搜索结果,再往上加UItableView


 

最正规的办法,用通知
step 1:
在进入视图的时候添加监视:(viewDidLoad什么的)
复制代码
// Observe keyboard hide and show notifications to resize the text view appropriately.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

step 2:
在键盘动作的时候移动视图:
复制代码
- (void)keyboardWillShow:(NSNotification *)notification {
   
    /*
     Reduce the size of the text view so that it‘s not obscured by the keyboard.
     Animate the resize so that it‘s in sync with the appearance of the keyboard.
     */
    NSDictionary *userInfo = [notification userInfo];
   
    // Get the origin of the keyboard when it‘s displayed.
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    // Get the top of the keyboard as the y coordinate of its origin in self‘s view‘s coordinate system. The bottom of the text view‘s frame should align with the top of the keyboard‘s final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
   
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
   
    // Get the duration of the animation.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
   
    // Animate the resize of the text view‘s frame in sync with the keyboard‘s appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
   
    textView.frame = newTextViewFrame;
    [UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
   
    NSDictionary* userInfo = [notification userInfo];
   
    /*
     Restore the size of the text view (fill self‘s view).
     Animate the resize so that it‘s in sync with the disappearance of the keyboard.
     */
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
   
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
   
    textView.frame = self.view.bounds;
   
    [UIView commitAnimations];
}

step 3:
在退出视图的时候注销通知
viewDidUnload:
复制代码
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

dealloc:
复制代码
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];

这些代码是摘自apple sample code KeyboardAccessory.
些许细节自己修改下就好了,比如那个textView


- (BOOL)textFieldDidBeginEditing:(UITextField *)textField
{
    UITableViewCell * cell=(UITableViewCell *)[[textField  superview] superview];
    NSIndexPath *indexPath=[curTable indexPathForCell:cell];
    if (indexPath.section==0) {
        
    }else {
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
        [UIView setAnimationDuration:0.30f];  
        point = curTable.center;
        curTable.center = CGPointMake(160, 120);
        [UIView commitAnimations];
    }
    return YES;
}

- (BOOL)textFieldDidEndEditing:(UITextField *)textField
{
    UITableViewCell * cell=(UITableViewCell *)[[textField  superview] superview];
    NSIndexPath *indexPath=[curTable indexPathForCell:cell];
    
    if (indexPath.section==0) {
        
    }else {
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
        [UIView setAnimationDuration:0.30f];  
        
        curTable.center = point;
        [UIView commitAnimations];
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    
    return YES;
}

 
时间: 2024-08-04 22:47:15

弹出键盘怎样把tableview往上顶?不遮住输入行的相关文章

让UIWebView弹出键盘上的按钮显示中文

UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的,有时我们想把按钮文字变为中文 其实办法很简单,只需要让你的应用程序支持中文本地化,意思是在项目中新建一个中文的本地化文件夹zh-Hans.lproj 如果还不太了解什么叫本地化,可以看看我的这篇文章<应用程序本地化> 下面简单演示下操作步骤: 1.添加中文本地化支持 2.选择要支持本地化的文件,至

弹出键盘时,让table向上移动

弹出键盘时,让table向上移动 #pragma mark UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing: (UITextField *)textField { [UIView beginAnimations: nil context: nil]; self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 280, 0); NSIndexPath *indexPath = [NSIn

IOS开发之自定义系统弹出键盘上方的view

IOS开发之自定义系统弹出键盘上方的view 分类: IOS 2014-11-18 09:26 1304人阅读 评论(0) 收藏 举报 目录(?)[+] 这篇文章解决的一个开发中的实际问题就是:当弹出键盘时,自定义键盘上方的view.目前就我的经验来看,有两种解决方法.一个就是利用 UITextField或者UITextView的inputAccessoryView属性,另一种,就是监听键盘弹出的notification来自 己解决相关视图的位置问题. 第一种解决方法相对比较简单,第二种的方法中

弹出键盘windowsoftinputmode属性设置值

windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifest.xml文件中界面对应的<activity>里加入            android:windowSoftInputMode="adjustPan"   键盘就会覆盖屏幕            android:windowSoftInputMode="state

Android弹出键盘布局闪动原理和解决

弹出键盘布局闪动原理和解决 在开发中,遇到一个问题:做一个微信一样,表情输入和软键盘在切换的时候,聊天界面不闪动的问题.为了解决这个问题,需要知道一下Android的软键盘弹出的时候发生的几个变化. 当AndroidMainfest.xml 中配置android:windowSoftInputMode="adjustResize|stateHidden" 属性后,如果弹出软键盘,那么会重绘界面.基本流程如下(API 10): 1.  Android 收到打开软键盘命令 2.  Andr

UI弹出键盘和收回键盘

点击textfield,会自动弹出键盘 要让键盘收回来,先设置个代理:[field setTextFieldDelegate:self];  可设置成自己,也可设置成其他对象,只要在对应的类中,遵循UITextFieldDelegate协议 在UITextFieldDelegate协议中,有一些可选的方法: //点击return回收键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstRe

关于android中EditText自动获取焦点并弹出键盘的相关设置

在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获取焦点并弹出键盘,代码: EditText.setFocusable(true); EditText.setFocusableInTouchMode(true); EditText.requestFocus(); 需求:EditText不会自动获取焦点并且不会弹出键盘,代码:  将其父控件设置: P

IPhone手机页面中点击文本输入框,弹出键盘,网页会放大,如何解决

在head标签中加入以上meta声明.具体属性可以谷歌/百度. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 我查了下viewport,有几个属性:width - viewport的宽度 height - viewport的高度initial-scale - 初始的缩放比例minim

获取弹出键盘时间,,,与高度

弹出键盘操作 //1.定制通知 //在初始化时定制通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //2.对应的方法. -(void)KeyboardWillShow:(NSNotification *)notification { NSDictionary