Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7

前端 未结 30 2491
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 12:47

Starting in iOS7, there is additional space at the top of my UITableView\'s which have a style UITableViewStyleGrouped.

Here is an example:<

相关标签:
30条回答
  • I'm assuming that is just part of the new UITableViewStyleGrouped styling. It is in all grouped table views and there doesn't seem to be any direct way to control that space.

    If that space is being represented by a UIView, it would be possible to search through all the subviews of the UITableView to find that specific view and edit it directly. However, there is also the possibility that that space is just a hardcoded offset before headers and cells start and there won't be any way to edit it.

    To search through all subviews (I would run this code when the table has no cells, to make it a little easier to read the output):

    - (void)listSubviewsOfView:(UIView *)view {
    
        // Get the subviews of the view
        NSArray *subviews = [view subviews];
    
        // Return if there are no subviews
        if ([subviews count] == 0) return;
    
        for (UIView *subview in subviews) {
    
            NSLog(@"%@", subview);
    
            // List the subviews of subview
            [self listSubviewsOfView:subview];
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:21

    Swift 4 code: For tableview with no section headers you can add this code:

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    

    and you will get the header spacing to 0.

    If you want a header of your specific height pass that value:

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return header_height
    }
    

    and the view from viewForHeaderinSection delegate.

    0 讨论(0)
  • 2020-11-22 13:22

    I played around with it a bit more and it seems like this is a side-effect of setting the tableView's tableHeaderView = nil.

    Because my tableView has a dynamically appearing tableHeaderView, when I need to hide the tableHeaderView, instead of doing self.tableView.tableHeaderView = nil;, I do:

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];
    

    I like this solution better than setting a somewhat arbitrary contentInset.top because I use the contentInset.top dynamically as well. Having to remember to remove an extra 35px whenever I recalculate contentInset.top is tedious.

    0 讨论(0)
  • 2020-11-22 13:22

    My answer is going to be more general answer, but can be applied on this as well.

    If the root view (of the ViewController) or the first child (subview) of the root view is subclass of the UIScrollView (or UIScrollView itself), and if

    self.navigationController.navigationBar.translucent = YES;
    

    framework will automatically set pre-calculated contentInset.


    To avoid this you can do

    self.automaticallyAdjustsScrollViewInsets = NO;
    

    but in my case I wasn't able to do this, because I was implementing SDK which has UIView component which can be used by other developers. That UIView component contains UIWebView (which has UIScrollView as the first subview). If that component is added as the first child in the UIViewController's view hierarchy, automatic insets will be applied by system.

    I've fixed this by adding dummy view with frame (0,0,0,0) before adding UIWebView.

    In this case system didn't find subclass of the UIScrollView as the first subview and didn't apply insets

    0 讨论(0)
  • 2020-11-22 13:23

    Try changing the contentInset property that UITableView inherits from UIScrollView.

    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    

    It's a workaround, but it works

    0 讨论(0)
  • 2020-11-22 13:23

    Swift: iOS I had tableview on scroll view .. when I was click "Back" on the same screen. Scroll view take more space on top.. to solve this I have used :

     self.automaticallyAdjustsScrollViewInsets = false
    

    A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets. Default value is true, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to false if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

    0 讨论(0)
提交回复
热议问题