0

I dragged a "Label" object into a scene in my storyboard in Xcode's Interface Builder.

The problem is that the Label is "pinned" to the top of the screen, so that when I scroll down in the View Controller, the Label is always located at the top of the screen.

What I want instead is for the Label to disappear when I scroll down and re-appear when I scroll back up. I don't want the Label to scroll at all. I want its position to be completely fixed.

I found this answer but I don't recognize the screen capture in Xcode 8.2.1.

Here is the View Controller structure:

image

Crickets
  • 524
  • 1
  • 8
  • 23

2 Answers2

1

Try this:

@interface ViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) IBOutlet UILabel *fadingLabel;

@end

@implementation ViewController

#pragma mark - UIScrollViewDelegate

/* For this method to get called, make sure you set the delegate of your table view to this view controller.*/

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!self.fadingLabel)
        return;

    CGFloat labelHeight = CGRectGetHeight(self.fadingLabel.frame);
    CGFloat alpha = 1.0f - (scrollView.contentOffset.y / labelHeight);
    [self.fadingLabel setAlpha:alpha];
}

@end

Here's the swift translation:

class ViewController: UIViewController, UIScrollViewDelegate {
    private weak var fadingLabel: UILabel?

    // MARK: - UIScrollViewDelegate

    /* For this method to get called, make sure you set the delegate of your table view to this view controller.*/
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard let label = self.fadingLabel else {
            return
        }

        let labelHeight: CGFloat = label.frame.height
        let alpha: CGFloat = 1.0 - (scrollView.contentOffset.y / labelHeight)
        label.alpha = alpha
    }
}
Pranay
  • 846
  • 6
  • 10
  • Also, if you want to have a slower fading effect on your label as a user scrolls, multiply the `labelHeight` by a number. Something like: `CGFloat labelHeight = CGRectGetHeight(self.fadingLabel.frame) * 2.0f; // or 3.0f` and that should slow down the fading effect on the label. – Pranay Apr 19 '18 at 21:19
  • My Xcode project utilizes Swift 3. I've tried [this tool](https://objectivec2swift.com/#/converter/), but I'm still having difficulty getting this code to compile in Swift without errors. Any help is appreciated. – Crickets Apr 20 '18 at 00:26
0

You should implement UIScrollViewDelegate and hide/show the label according to the vertical content offset of your scroll view in scrollViewDidScroll(_:).

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    label.isHidden = scrollView.contentOffset.y > 50
}

Don't forget to set the delegate of your scroll view:

scrollView.delegate = self
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • This is almost perfect. The problem is that when I scroll down just a little bit, the Label's position stays exactly the same, which is a bit "unrealistic." I would prefer if it appeared that the label was actually a "part" of the View Controller, so when you scroll down, the label naturally slides out from view, as opposed to abruptly being hidden. – Crickets Apr 19 '18 at 19:08
  • @Crickets Perhaps [this](https://stackoverflow.com/questions/35385856/how-to-make-a-label-fade-in-or-out-in-swift) answer would help. – Kylo-Rey Apr 19 '18 at 19:47