How to make UITable header stick to the top of the screen when using Storyboards?

后端 未结 3 1532
感情败类
感情败类 2020-12-31 20:18

I\'ve made header using this post: Table Header Views in StoryBoards

But i am unable to make header stick (fix) to the top of the screen while scrolling.

How

相关标签:
3条回答
  • 2020-12-31 20:55

    Here's the code which I believe makes the header view stick to the table top (that is not it's default behavior):

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGRect rect = self.tableHeaderView.frame;
        rect.origin.y = MIN(0, self.tableView.contentOffset.y);
        self.tableHeaderView.frame = rect;
    }
    

    An alternative is to have header view for the first section. You can't use any subview at viewForHeaderInSection, you have to return there the view which is not yet at views hierarchy. The advantage of the method is that the section header view is designed to be at the table top, the drawback is that you might want to have headers for sections in the feature, it will break the solution.

    0 讨论(0)
  • 2020-12-31 21:18

    I was able to do it (at least in iOS 6) like this, don't know why this thing works:)

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        if(self.headerManualView == nil) {
            //allocate the view if it doesn't exist yet
            headerManualView  = [[UIView alloc] init];
    
            //create the button
            UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 250, 44)];
    
            //the button should be as big as a table view cell
            txtField.borderStyle = UITextBorderStyleRoundedRect;
    
            //set action of the button
            //[txtField addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];
    
            //add the button to the view
            [headerManualView addSubview:txtField];
        }
    
        //return the view for the footer
        return headerManualView;
    }
    
    0 讨论(0)
  • 2020-12-31 21:20

    I think it's better for you to use a UIViewController, instead of a UITableViewController in the storyboard, to achieve this. Just put a header in the top of the view, in the storyboard, and put a UITableView under the header.

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