How to resize a tableHeaderView of a UITableView?

后端 未结 19 2208
死守一世寂寞
死守一世寂寞 2020-11-29 16:14

I\'m having trouble resizing a tableHeaderView. It simple doesn\'t work.

1) Create a UITableView and UIView (100 x 320 px);

2) Set the UIView as tableHeaderV

相关标签:
19条回答
  • 2020-11-29 16:46

    Did you try [self.tableView reloadData] after changing the height?

    0 讨论(0)
  • 2020-11-29 16:47

    This worked for me on iOS 7 and 8. This code is running on the table view controller.

    [UIView animateWithDuration:0.3 animations:^{
        CGRect oldFrame = self.headerView.frame;
        self.headerView.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, oldFrame.size.width, newHeight);
        [self.tableView setTableHeaderView:self.headerView];
    }];
    
    0 讨论(0)
  • 2020-11-29 16:47

    UITableView resizing header - UISearchBar with Scope Bar

    I wanted a UITableView with a UISearchBar as the header to the table so I have a hierarchy that looks like this

    UITableView
      |
      |--> UIView
      |     |--> UISearchBar
      |
      |--> UITableViewCells
    

    UISearchBarDelegate methods

    As has been stated elsewhere, if you don't setTableViewHeader after changing it, nothing will happen.

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
    {
        searchBar.showsScopeBar = YES;
        [UIView animateWithDuration:0.2f animations:^{
            [searchBar sizeToFit];
            CGFloat height = CGRectGetHeight(searchBar.frame);
    
            CGRect frame = self.tableView.tableHeaderView.frame;
            frame.size.height = height;
            self.tableHeaderView.frame = frame;
            self.tableView.tableHeaderView = self.tableHeaderView;
        }];
    
        [searchBar setShowsCancelButton:YES animated:YES];
        return YES;
    }
    
    - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
    {
        searchBar.showsScopeBar = NO;
        [UIView animateWithDuration:0.f animations:^{
            [searchBar sizeToFit];
    
            CGFloat height = CGRectGetHeight(searchBar.frame);
    
            CGRect frame = self.tableView.tableHeaderView.frame;
            frame.size.height = height;
            self.tableHeaderView.frame = frame;
            self.tableView.tableHeaderView = self.tableHeaderView;
        }];
    
        [searchBar setShowsCancelButton:NO animated:YES];
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-29 16:54

    Used @garrettmoon solution above until iOS 7.
    Here's an updated solution based on @garrettmoon's:

    - (void)adjustTableHeaderHeight:(NSUInteger)newHeight animated:(BOOL)animated {
    
        [UIView beginAnimations:nil context:nil];
    
        [UIView setAnimationDuration:[CATransaction animationDuration]];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    
        self.frame = CGRectMake(self.frame.origin.x,
                            self.frame.origin.y,
                            self.frame.size.width,
                            newHeight);
    
        [(UITableView *)self.superview setTableHeaderView:self];
    
        [UIView commitAnimations];
    }
    
    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
        [(UITableView *)self.superview setTableHeaderView:self];
    }
    
    0 讨论(0)
  • 2020-11-29 16:57

    I found the initWithFrame initializer of a UIView doesn't properly honor the rect I pass in. Hence, I did the following which worked perfectly:

     - (id)initWithFrame:(CGRect)aRect {
    
        CGRect frame = [[UIScreen mainScreen] applicationFrame];
    
        if ((self = [super initWithFrame:CGRectZero])) {
    
            // Ugly initialization behavior - initWithFrame will not properly honor the frame we pass
            self.frame = CGRectMake(0, 0, frame.size.width, 200);
    
            // ...
        }
    }
    

    The advantage of this is it is better encapsulated into your view code.

    0 讨论(0)
  • 2020-11-29 16:58

    On iOS 9 and below, tableHeaderView would not re-layout after resizing it. This issue is resolved in iOS 10.

    To solve this issue, just do it with the following code:

    self.tableView.tableHeaderView = self.tableView.tableHeaderView;
    
    0 讨论(0)
提交回复
热议问题