Remove empty space before cells in UITableView

后端 未结 30 1746
生来不讨喜
生来不讨喜 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:12

    If you are doing this on iOS 11, automaticallyAdjustsScrollViewInsets won't work as it is deprecated, the following code worked for me as per the answer in this post: https://stackoverflow.com/a/44391269/5476481

            if #available(iOS 11.0, *) {
                tblView.contentInsetAdjustmentBehavior = .never
            } else {
                automaticallyAdjustsScrollViewInsets = false
            }
    

    Hope this helps.

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

    Well for me the problem occurred when I dragged a prototype cell from the menu into the table view. So I deleted that and just set prototype cell to 1 in table view inspector properties

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

    You can use this code into viewDidLoad or viewDidAppear where your table being created:

    // Remove blank space on header of table view
     videoListUITableView.contentInset = UIEdgeInsetsZero;
    
    // The iOS device = iPhone or iPod Touch
    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
    
    // Set the height of the table on the basis of number of rows
    videoListUITableView.frame = CGRectMake(videoListUITableView.frame.origin.x, videoListUITableView.frame.origin.y, videoListUITableView.frame.size.width, iOSDeviceScreenSize.height-100);
    
    
    
    // Hide those cell which doesn't contain any kind of data
    self.videoListUITableView.tableFooterView = [[UIView alloc] init];
    
    0 讨论(0)
  • 2020-11-30 17:13

    In My Case I had a UILabel under the UITableView in view hierarchy.

    I moved it "forward" and the blank space appeared. Not sure why but it works like this, if theres anything under the tableView, it hides the blank space.

    Also you can try checking/uncheking "Adjust Scroll View Insets" on your view controller inspector on storyboard.

    enter image description here

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

    In my application also I've experienced an issue like this. I've set top edge inset of tableView zero. Also tried set false for automaticallyAdjustsScrollViewInsets. But didn't work.

    Finally I got a working solution. I don't know is this the correct way. But implementing heightForHeaderInSection delegate method worked for me. You have to return a non-zero value to work this (return zero will display same space as before).

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
        return 0.1
    }
    
    0 讨论(0)
  • 2020-11-30 17:13

    In iOS 11 and above, apple has changed property to adjust content inset. Use contentInsetAdjustmentBehavior and set .never to remove extra space above UICollectionView, UIScrollView, UITableView.

    if #available(iOS 11.0, *) {
    
        // For scroll view
        self.scrollView.contentInsetAdjustmentBehavior = .never
    
        // For collection view
        self.collectionView.contentInsetAdjustmentBehavior = .never
    
        // For table view
        self.tableView.contentInsetAdjustmentBehavior = .never
    
    } else {
        self.automaticallyAdjustsScrollViewInsets = false
    }
    

    I hope this will help you.

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