1

In my View controller i have 1 tableview with different Data.

Top Blue colour is View & bottom Blue colour is TabBar.

in Home Screen there's top View & in Trending screen i Hide it by changing it's constraint to 0.

  1. Home and 2. Trending

When i scroll tableview to see new posts & when i click on Trending View i load new data in TableView & scroll tableview to top with the code.

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tblVW setContentOffset:CGPointZero animated:NO];
        [self.tblVW reloadData];
    });

but it will not move to top of tableview.

There's a space at top & when i touch tableview it will scroll to top.

Pankil
  • 635
  • 6
  • 20
  • check this similar post https://stackoverflow.com/questions/43023384/uicollectionview-remove-top-padding/43025517#43025517 – Joe May 25 '17 at 08:23

6 Answers6

13

In a grouped tableview,If you want to remove the default header in the top area, try setting:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.1))

Grouped TableView:

Grouped TableView

Before:

Before

After:

After

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
rayjuneWu
  • 131
  • 1
  • 5
2

I guess you are using grouped tableview. Kindly make

self.tblVW.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0,0,0,0.01f)];

Even set height of tableview section header as 0.01f.

  • i return minimum height by return CGFLOAT_MIN; – Pankil May 25 '17 at 07:39
  • Are you using grouped tableview? @Pankil. Can you please change it to 0.01f instead of CGFLOAT_MIN? i guess it should work. – Dhaval Soni May 25 '17 at 07:40
  • yes, home tableview is grouped tableview & trending tableview is plain. – Pankil May 25 '17 at 07:43
  • Set the tableheader view as the self.tblVW.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0,0,0,0.01f)]; for home tableview. it will remove the top space. . – Dhaval Soni May 25 '17 at 07:44
  • For grouped tableview if you have observed settings app in iphone it has default space at the top. So to fix it we need to set tableheaderview with frame(0,0,0,0.01f) and even set section header height as 0.01f as 0.0f won't work in that case. Kindly check it at your end and let me know the result. Thanks. – Dhaval Soni May 25 '17 at 07:56
1
YourTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

Set Above code.

Joe
  • 8,868
  • 8
  • 37
  • 59
Rakesh Patel
  • 1,673
  • 10
  • 27
0

You need to disable automaticallyAdjustsScrollViewInsets .

add this line ViewDidLoad

self.automaticallyAdjustsScrollViewInsets = false
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • I already unCheck it from Storyboard, but still i am facing this issue – Pankil May 25 '17 at 07:32
  • when i scroll trending data to bottom and than i click on home, it will scroll to top. but when i am on home screen and click on trending tableview not scroll to top automatically. – Pankil May 25 '17 at 07:37
  • 1
    try also this : tableView.contentInset = UIEdgeInsets.zero – KKRocks May 25 '17 at 08:10
0

Try this to scroll the table view to top

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tblVW.scrollToRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: true)];
        [self.tblVW reloadData];
    });

instead of setting content offset.

Ravi
  • 2,441
  • 1
  • 11
  • 30
0

This works for me, put this code inside the viewDidLoad() tableView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)

Example:

override func viewDidLoad() {
   super.viewDidLoad()
   self.setupTableView() 
}

func setupTableViews() {
   tableView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0) 
}