How to position UITableViewCell at bottom of screen?

前端 未结 14 610
旧时难觅i
旧时难觅i 2020-12-08 03:25

There are one to three UITableViewCells in a UITableViewView. Is there a way to always position the cell(s) at the bottom of screen after rel

14条回答
  •  渐次进展
    2020-12-08 03:45

    The best way of doing this I found is to observe the content size of the tableview and adjust the inset if necessary. For example:

    static char kvoTableContentSizeContext = 0;
    
    - (void) viewWillAppear:(BOOL)animated
    {
        [_tableView addObserver:self forKeyPath:@"contentSize" options:0 context:&kvoTableContentSizeContext];
    }
    
    - (void) viewWillDisappear:(BOOL)animated
    {
        [_tableView removeObserver:self forKeyPath:@"contentSize"];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if (context == &kvoTableContentSizeContext) {
    
            CGFloat contentHeight = _tableView.contentSize.height;
            CGFloat tableHeight = _tableView.frame.size.height;
    
            if (contentHeight < tableHeight) {
    
                UIEdgeInsets insets = _tableView.contentInset;
    
                insets.top = tableHeight - contentHeight;
                _tableView.contentInset = insets;
    
            } else {
                _tableView.contentInset = UIEdgeInsetsZero;
            }
    
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    

提交回复
热议问题