Remove empty space before cells in UITableView

后端 未结 30 1749
生来不讨喜
生来不讨喜 2020-11-30 16:39

I am currently trying to put a UITableView in a different location rather than at the top of my view controller. With this said, it is trying to add the header

相关标签:
30条回答
  • 2020-11-30 17:13

    I just found a solution for this. In my case, i was using TabBarViewController, A just uncheck the option 'Adjust Scroll View Insets'. Issues goes away. https://i.stack.imgur.com/vRNfV.png

    0 讨论(0)
  • 2020-11-30 17:15

    Do the cells of the UITableView show on the empty space when you scroll down?

    If so, then the problem might be the inset that is added to the UITableView because of the Navigation controller you have in your view. The inset is added to the table view in order for the content to be placed below the navigation bar when no scrolling has occurred. When the table is scrolled, the content scrolls and shows under a transparent navigation bar. This behavior is of course wanted only if the table view starts directly under the navigation bar, which is not the case here.

    Another thing to note is that iOS adjusts the content inset only for the first view in the view hierarchy if it is UIScrollView or it's descendant (e.g. UITableView and UICollectionView). If your view hierarchy includes multiple scroll views, automaticallyAdjustsScrollViewInsets will make adjustments only to the first one.

    Here's how to change this behavior:

    a) Interface Builder

    • Select the view controller
    • Open Attributes inspector
    • There's a property called "Adjust scroll view insets" in IB's attribute inspector (when a view controller is selected) which is on by default. Uncheck this option:


      (Image courtesy of Dheeraj D)

    I'm not sure which Xcode version introduced this option (didn't spot it in the release notes), but it's at least available in version 5.1.1.

    Edit: To avoid confusion, this was the third option mentioned in the comments

    b) Programmatically

    Add this to i.e. viewDidLoad (credits to Slavco Petkovski's answer and Cris R's comment)

    // Objective-C
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // Swift
    self.automaticallyAdjustsScrollViewInsets = false
    

    c) This might be relevant for old schoolers

    You can either fix this by adding

    tableView.contentInset = UIEdgeInsetsZero
    
    //Swift 3 Change
    tableView.contentInset = UIEdgeInsets.zero
    

    Or if you are using IB and if the navigation bar is not transparent (can't tell from the screenshot)

    • Select the view controller
    • Open Attributes inspector
    • In View Controller options Extend Edges section deselect "Under Top Bars"
    0 讨论(0)
  • 2020-11-30 17:16

    There is may be pre-added UIRefreshControl. I have this problem with code like this:

    self.refreshControl = [[UIRefreshControl alloc] init];{
        [self.refreshControl setAttributedTitle:[[NSAttributedString alloc]
                                                 initWithString:@"Your string"]];
        [self.refreshControl addTarget:self
                                action:@selector(updateScreen)
                      forControlEvents:UIControlEventValueChanged];
    
    }
    

    As you see, I set self.refreshController immediately after init

    But, if you set refreshControl after setup, then top-space don't interfere you.

    UIRefreshControl *refreshControl  = [[UIRefreshControl alloc] init];{
        [self.refreshControl setAttributedTitle:[[NSAttributedString alloc]
                                                 initWithString:@"Your String"]];
        [self.refreshControl addTarget:self
                                action:@selector(updateScreen)
                      forControlEvents:UIControlEventValueChanged];
        self.refreshControl = refreshControl;
    }
    
    0 讨论(0)
  • 2020-11-30 17:16

    set ViewController's Extended Edges in storyboard so that underTopBar property is unchecked.

    0 讨论(0)
  • 2020-11-30 17:17

    Xcode 8 bug…

    Try this first if your Storyboard UITableView style is set to Plain.

    Set the table's style to Grouped and then back to Plain. This removed the space in my app at the head of my UITableView.

    0 讨论(0)
  • 2020-11-30 17:19

    check for

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    • mention tableview delegate function cellForRowAtIndexPath:
    • it can handled both ios 6 and 7.
    0 讨论(0)
提交回复
热议问题