繁体   English   中英

以编程方式更改UITableView的滚动插入以反映iPhone键盘的大小

[英]Changing a UITableView's scroll insets programmatically to reflect the size of an iPhone keyboard

我知道您可以使用Interface Builder中的“Inset”属性使滚动视图从主窗口嵌入,这样它就不会低于屏幕上的现有控件(如标签栏),但是如何以编程方式执行此操作调整键盘何时添加到屏幕? 目前,我的滚动视图在键盘下方有单元格无法访问,因为视图仍然将滚动视图的底部注册为手机底部,而不是键盘顶部。

#pragma mark Keyboard Handling



- (void) viewDidAppear:(BOOL) ani {
    onscreen = YES;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardDidAppear:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewDidDisappear:(BOOL) ani {
    onscreen = NO;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) keyboardWillAppear:(NSNotification*) n {
    NSLog(@"Keyboard is about to appear");
}

- (void) keyboardDidAppear:(NSNotification*) n {

    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height -= bounds.size.height; // subtract the keyboard height
    if (self.tabBarController != nil) {
        tableFrame.size.height += 48; // add the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;
    [UIView commitAnimations];

    //[self hideEditorView:currentEditorView];
    //[currentEditorView removeFromSuperview];
}

- (void) shrinkDidEnd:(NSString*) ident finished:(BOOL) finished contextInfo:(void*) nothing {
    NSIndexPath* sel = [searchResultsTable indexPathForSelectedRow];

    if (![[searchResultsTable indexPathsForVisibleRows] containsObject:sel])
    {
        [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

- (void) keyboardWillDisappear:(NSNotification*) n {
    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height += bounds.size.height; // add the keyboard height

    if (self.tabBarController != nil) {
        tableFrame.size.height -= 48; // subtract the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;    
    [UIView commitAnimations];

    [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

两种选择。 (当你说滚动视图我认为你的意思是表格视图,因为标题暗示如此)

  1. 使用UITableViewController ,如果将键盘添加到表视图中,我相信它会为您调整大小

  2. - (void)textFieldDidBeginEditing:(UITextField *)textField方法的textfield或textbox协议中,调整表格视图框架的大小并完成编辑,然后将其设置回(如果它是滚动视图而不是的tableView)

    • (void)textFieldDidBeginEditing:(UITextField *)textField {CGRect frame = tableView.frame; //或滚动视图frame.size.height = frame.size.height- keyboardHeight; tableView.frame = frame}

    • (void)textFieldDidEndEditing:(UITextField *)textField {CGRect frame = tableView.frame; //或滚动视图frame.size.height = frame.size.height + keyboardHeight; tableView.frame = frame}

希望这可以帮助

使用Interface Builder或代码的另一种简单方法

将[ Table View Size Footer Height ]属性设置为200(为此布局使用此属性)。 这有效地使表格大于其所包含的边界框架,允许表格向上滚动高于正常。

非常适合让用户根据需要上下滚动表格。

我确实创建了一个用键盘解决这个问题的小项目,在我的情况下我只需要在键盘出现时使表格视图上升。

希望这可以帮助!

http://git.io/BrH9eQ

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM