Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like the other answers here are providing solutions for a notification that drops down below the UINavigationBar. If you're still looking for a solution that sits in the scrollview of the UITableView, then I would add a custom table header (not section header) to the table view. </p> <p>Here are the rough steps necessary to accomplish this:</p> <p><strong>1. Create the initial header view on load</strong></p> <p>I typically use a UIViewController subclass that owns a UITableView instance variable (instead of using a UITableViewController), but you should be able to accomplish this with either set up. In your tableview set up code (probably in viewDidLoad), where you set things like backgroundColor, contentInset, separatorStyle, etc, create a UILabel that will become your header. Then set this UILabel to be the tableHeaderView of your tableView. Of course, if you're looking to make something a bit more complicated for this "notification section", feel free to make it a UIView with a nested UILabel + something else. So something like:</p> <pre><code>UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 44.0f)]; headerLabel.backgroundColor = [UIColor clearColor]; // Just in case you have some fancy background/color in your table view headerLabel.textAlignment = UITextAlignmentCenter; headerLabel.font = .... headerLabel.textColor = .... headerLabel.text = @"You are an awesome user!"; self.tableView.tableHeaderView = headerLabel; </code></pre> <p><strong>2. Set your tableview to load "normally" (ie. not show header)</strong></p> <p>Again, inside viewDidLoad, you need to set your tableview's contentOffset and alwaysBounceVertical properly to hide this header view on load. contentOffset set to the height of the header will start the tableview's y coordinate right below the header. alwaysBounceVertical set to YES will allow your tableview to behave correctly even if your tableview's contentsize is less than your screen size. So something like:</p> <pre><code>self.tableView.contentOffset = (CGPoint){0.0f, 44.0f}; self.tableView.alwaysBounceVertical = YES; </code></pre> <p><strong>3. Add the slide down and slide away</strong></p> <p>Ok, now there are a couple of ways you can do this. On viewDidAppear, you can create a chained UIView animation where the first animation slides the tableview down (ie. sets contentOffset to {0.0f, 0.0f}) is delayed by one second and the second animation slides the tableview back up (ie. sets contentOffset to {0.0f, 44.0f}) is delayed by two seconds. Or you can use GCD and schedule the two animations as async + delayed blocks. Either way is fine (and there are probably two or three other good ways to accomplish this), but just to get the idea across... you can chain the animation like this:</p> <pre><code>__weak MyCustomViewController *me = self; [UIView animateWithDuration:0.4f delay:1.0f options:UIViewAnimationOptionAllowUserInteraction animations:^ { me.tableView.contentOffset = (CGPoint){0.0f, 0.0f}; } completion:^(BOOL finished) { if (me.tableView) { [UIView animateWithDuration:0.4f delay:2.0f options:UIViewAnimationOptionAllowUserInteraction animations:^ { me.tableView.contentOffset = (CGPoint){0.0f, 44.0f}; } completion:^(BOOL finished) { if (me.tableView) { me.tableView.tableHeaderView = nil; // If you want to completely get rid of this notification header me.tableView.contentOffset = (CGPoint){0.0f, 0.0f}; // I'm unsure if this will cause the tableview to jump... if it does, you can animate the headerview away instead of animating the tableview up to hide the header, then setting header to nil and reseting the contentOffset // or me.tableView.tableHeaderView.hidden = YES; // If you want to just hide the header } }]; } }]; </code></pre> <p>I wrote the sample code without testing any of it... hehe, so it probably won't work as it. But happy to help you flesh this out if you need more help! Good luck.</p> <p><strong>Update: Allowing user scrolling to cancel animation</strong></p> <p>I'm not too sure what you want the user interaction on the table to do to the animation. If you just want the animation to be canceled when the user starts scrolling, then I would use GCD (see code below). But I can see other ways you can have the animation work with user touch, so it depends on what you're looking for. In any case, let's say any user touch should disable the next scheduled animation then it could be achieved using two functions like:</p> <pre><code>- (void)scheduleShowHeaderAnimation { __weak MyCustomViewController *me = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0f * NSEC_PER_SEC), dispatch_get_main_queue(), ^ // Start animating after 1 sec delay { if (me.tableView) // Make sure the tableview is still around { if (! me.tableView.tracking &amp;&amp; // Don't show animation if user has begun to touch contentview ! me.tableView.dragging &amp;&amp; // Don't show animation if user has begun to drag contentview ! me.tableView.decelerating) // Don't show animation if dragging happened and scrollview is starting to decelerate { [UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionAllowAnimatedContent // This will make sure user can interact with tableview while animation is going on animations:^ { me.tableView.contentOffset = (CGPoint){0.0f, 0.0f}; } completion:^(BOOL finished) { [me scheduleHideHeaderAnimation]; }]; } } }); } - (void)scheduleHideHeaderAnimation { __weak MyCustomViewController *me = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0f * NSEC_PER_SEC), dispatch_get_main_queue(), ^ // Start animating after 2 secs delay { if (me.tableView) // Make sure the tableview is still around { if (! me.tableView.tracking &amp;&amp; // Don't show animation if user has begun to touch contentview ! me.tableView.dragging &amp;&amp; // Don't show animation if user has begun to drag contentview ! me.tableView.decelerating) // Don't show animation if dragging happened and scrollview is starting to decelerate { [UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionAllowAnimatedContent // This will make sure user can interact with tableview while animation is going on animations:^ { me.tableView.contentOffset = (CGPoint){0.0f, 44.0f}; } completion:^(BOOL finished) { if (me.tableView) { me.tableView.tableHeaderView = nil; // If you want to completely get rid of this notification header me.tableView.contentOffset = (CGPoint){0.0f, 0.0f}; // I'm unsure if this will cause the tableview to jump... if it does, you can animate the headerview away instead of animating the tableview up to hide the header, then setting header to nil and reseting the contentOffset // or me.tableView.tableHeaderView.hidden = YES; // If you want to just hide the header } }]; } } }); } </code></pre> <p>I would call scheduleShowHeaderAnimation in the viewDidAppear.</p> <p>Then to support hiding the header when the user has already scrolled the tableview down, I would implement either <code>- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate</code> or <code>- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView</code> of UIScrollViewDelegate and add something like:</p> <pre><code>if (self.tableView.tableHeaderView != nil) { self.tableView.tableHeaderView = nil; } // or if (self.tableView.tableHeaderView.hidden == NO) { self.tableView.tableHeaderView.hidden = YES; } </code></pre> <p>If there's more complicated interactions you want to support, or if you want the animation to respond to the user touch in different ways, you might need to override other UIScrollViewDelegate methods and when the user begins to interact with the scrollview (which is the parent class of the table view), then change the behavior of the animation.</p> <p>Does this get you closer to what you're looking for?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload