Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a view on top of UITableView that scrolls together, but stick to top after scrolling

I have a UIView with height of 100 pixels that's on top of my UITableView. When I scroll up I want the UIView to scroll together with my UITableView as if it's part it. When 50 pixels of my UIView is hidden from scrolling up, I want to pin the UIView at the top while I continue scrolling up. How can this be achieved? I tried using changing my UIView's top NSLayoutConstraint constant equal to my tableview's content offset, but they don't scroll at the same speed.

like image 511
iamarnold Avatar asked Apr 04 '17 16:04

iamarnold


People also ask

How do I embed a view in scroll view?

Embed ScrollView via : Editor -> Embed In -> Scroll View Now, create its outlet to the ViewController and add its content size. ScrollView's height must be bigger than view's frame size!

How do I get top of Tableview?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

Can UITableView scroll horizontal?

Using the horizontally scrolling cells of the UITableView enables us to develop a highly intuitive interface. It also enables the user to glance a lot of information (9 titles in the picture above).


2 Answers

First make sure your tableView is grouped:

self.tableView = UITableView(frame: CGRect(x: 0, y: (self.navigationController?.navigationBar.frame.maxY)!, width: self.view.bounds.size.width, height: (self.view.bounds.size.height - (self.navigationController?.navigationBar.frame.maxY)!)), style: .grouped)
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)  

Then you need to add the UIView into the subview of your tableView:

self.myView = UIView()
self.myView.backgroundColor = UIColor.green
self.myView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 100)
self.tableView.addSubview(self.myView)

Add a header of height 100 for your tableView's first section so that your cells are at right place:

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

Then adjust the frame of your UIView when scrolling:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset.y
    if(offset > 50){
        self.myView.frame = CGRect(x: 0, y: offset - 50, width: self.view.bounds.size.width, height: 100)
    }else{
        self.myView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 100)
    }
}

Demo:

enter image description here

like image 139
jokeman Avatar answered Oct 19 '22 18:10

jokeman


If I understand you'r question correctly, You can do it by implementing table view scrollViewDidScroll: method like this

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    static CGFloat previousOffset;
    CGRect rect = self.yourUIView.frame;

   //NSLog you'r UIView position before putting any condition 
   // NSLog(@"Origin %f",rect.origin.y);

    rect.origin.y += previousOffset - scrollView.contentOffset.y;
    previousOffset = scrollView.contentOffset.y;

    //assuming you'r UIView y position starts from 0
    //Before setting the condition please make sure to run without any 
    //condition if not working

    if(rect.origin.y>=-50 && rect.origin.y<=0){
            self.yourUIView.frame = rect;
    }
}

Hope it helps...

like image 24
Mahbub Ahmed Avatar answered Oct 19 '22 19:10

Mahbub Ahmed