Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:28:02+00:00 2026-06-13T11:28:02+00:00

I’m using the following code to hide a view and the space taken by

  • 0

I’m using the following code to hide a view and the space taken by the view based on a condition in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {

    Data* data = [Data shared];

    if (data.something == 0) {

        CGRect frame = self.tableView.tableHeaderView.frame;
        frame.size.height = 0;
        self.tableView.tableHeaderView.frame = frame;

        self.tableView.tableHeaderView.hidden = YES;

    } else {

        CGRect frame = self.tableView.tableHeaderView.frame;
        frame.size.height = 44;
        self.tableView.tableHeaderView.frame = frame;

        self.tableView.tableHeaderView.hidden = NO;

    }

}

The above code works, but I’m pretty sure that is not the right way to do that. I tried to set the tableHeaderView to nil, but once the code is called, the headerView is gone until the UITableView is destroyed (I think I can fix it using a IBOutlet to the tableHeader, but doesn’t sounds right too.

UPDATE1: another try, but the code doesn’t work:

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
   self.tableView.tableHeaderView.hidden = YES;
   return 0; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:28:04+00:00Added an answer on June 13, 2026 at 11:28 am

    The data source method tableView:heightForHeaderInSection: actually has nothing to do with the view that is associated with the table view’s tableViewHeader property. There are two different types of headers here, the one header at the top of the tableView, in which can be placed things like a search bar, and the multiple headers that can be made to occur one per section within the table view.

    To my knowledge, the tableViewHeader view is typically configured in the nib file, and I don’t know that the table view calls any data source methods that allow any configuration for it, so you would have to do it manually. Frankly, if your code works, that would be a good way to do it. Hiding it would make the table view still act as if it’s there…removing it entirely makes it so you can’t get it back because it gets deallocated.

    (However, as you said, you could use an IBOutlet pointing to the header view, as long as you make it a strong reference, and then you could somehow reinsert it into the table later. …Hm, although the mechanics of how you add a view into the table view’s scroll view, and position it correctly, is probably just annoying.)

    My only suggestion would be animating the frame height to zero so you get a nice transition effect, something like animateWithDuration. But yeah, I would say you have the best method figured out already.

    EDIT:

    Code, you say? I take that as a challenge 🙂

    - (void)setTableViewHeaderHidden:(BOOL)hide
    {
    
        // Don't want to muck things up if we are mid an animation.
        if (self.isAnimatingHeader) {
            return;
        }
    
        // This is our IBOutlet property, I am just saving a bit of typing.
        UIView *theHeader = self.theHeaderView;
    
        if (hide) {
    
            // Save the original height into the tag, should only be done once.
            if (!theHeader.tag) {
                theHeader.tag = theHeader.frame.size.height;
            }
    
            // Transform and hide
            if (theHeader.frame.size.height > 0) {
    
                self.isAnimatingHeader = YES;
    
                // New frame...
                CGRect frame = theHeader.frame;
                frame.size.height = 0;
    
                // Figure out some offsets here so we prevent jumping...
                CGPoint originalOffset = self.tableView.contentOffset;
    
                CGPoint animOffset = originalOffset;
                animOffset.y += MAX(0, theHeader.tag - animOffset.y);
    
                CGPoint newOffset = originalOffset;
                newOffset.y = MAX(0, newOffset.y - theHeader.tag);
    
                // Perform the animation
                [UIView animateWithDuration:0.35
                                      delay:0.0
                                    options: UIViewAnimationCurveEaseOut
                                 animations:^{
                                     theHeader.frame = frame;
                                     self.tableView.contentOffset = animOffset;
                                 }
                                 completion:^(BOOL finished){
                                     if (finished) {
    
                                         // Hide the header
                                         self.tableView.tableHeaderView = nil;
                                         theHeader.hidden = YES;
    
                                         // Shift the content offset so we don't get a jump
                                         self.tableView.contentOffset = newOffset;
    
                                         // Done animating.
                                         self.isAnimatingHeader = NO;
    
                                     }
                                 }
                 ];
    
            }
    
        } else {
    
            // Show and transform
            if (theHeader.frame.size.height < theHeader.tag) {
    
                self.isAnimatingHeader = YES;
    
                // Set the frame to the original before we transform, so that the tableview corrects the cell positions when we re-add it.
                CGRect originalFrame = theHeader.frame;
                originalFrame.size.height = theHeader.tag;
                theHeader.frame = originalFrame;
    
                // Show before we transform so that you can see it happen
                self.tableView.tableHeaderView = theHeader;
                theHeader.hidden = NO;
    
                // Figure out some offsets so we don't get the table jumping...
                CGPoint originalOffset = self.tableView.contentOffset;
    
                CGPoint startOffset = originalOffset;
                startOffset.y += theHeader.tag;
                self.tableView.contentOffset = startOffset; // Correct for the view insertion right off the bat
    
                // Now, I don't know if you want the top header to animate in or not. If you think about it, you only *need* to animate the header *out* because the user might be looking at it. I figure only animate it in if the user is already scrolled to the top, but hey, this is open to customization and personal preference.
    
                if (self.animateInTopHeader && originalOffset.y == 0) {
    
                    CGPoint animOffset = originalOffset;
    
                    // Perform the animation
                    [UIView animateWithDuration:0.35
                                          delay:0.0
                                        options: UIViewAnimationCurveEaseIn
                                     animations:^{
                                         self.tableView.contentOffset = animOffset;
    
                                     }
                                     completion:^(BOOL finished){
                                         // Done animating.
                                         self.isAnimatingHeader = NO;
                                     }
                     ];
    
                } else {
                    self.isAnimatingHeader = NO;
                }
    
            }
    
        }
    }
    

    Built this in the table view template that comes with Xcode. Just to throw it together I used a UILongPressGestureRecognizer with the selector outlet pointing to this method:

    - (IBAction)longPress:(UIGestureRecognizer *)sender
    {
        if (sender.state != UIGestureRecognizerStateBegan) {
            return;
        }
        if (self.hidingHeader) {
            self.hidingHeader = NO;
            [self setTableViewHeaderHidden:NO];
        } else {
            self.hidingHeader = YES;
            [self setTableViewHeaderHidden:YES];
        }
    
    }
    

    And, I added these to my header:

    @property (strong, nonatomic) IBOutlet UIView *theHeaderView;
    @property (nonatomic)         BOOL                       hidingHeader;
    @property (nonatomic)         BOOL                       isAnimatingHeader;
    @property (nonatomic)         BOOL                       animateInTopHeader;
    
    - (IBAction)longPress:(id)sender;
    

    Anyway, it works great. What I did discover is that you definitely have to nil out the table view’s reference to the header view or it doesn’t go away, and the table view will shift the cells’ position based on the height of the frame of the header when it is assigned back into its header property. Additionally, you do have to maintain a strong reference via your IBOutlet to the header or it gets thrown away when you nil out the table view’s reference to it.

    Cheers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.