Twitter-Like Scrolling Title

后端 未结 3 1621
离开以前
离开以前 2021-02-01 09:10

On the Twitter iOS app when you scroll past your name in the profile section below the navigation bar, your name begins to scroll into view on the navigation bar itself and stic

3条回答
  •  温柔的废话
    2021-02-01 10:08

    Here I got it working. This ViewController is presented in a simple UINavigationController.

    @interface ViewController () 
    {
        UIScrollView *titleView;
        UIScrollView *contentView;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        [self.view setBackgroundColor:[UIColor lightGrayColor]];
    
        [self setTitle:@"My Title"];
    
        titleView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 44.0)];
        [titleView setContentSize:CGSizeMake(0.0, 88.0)];
        [self.view addSubview:contentView];
    
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 44.0, CGRectGetWidth(titleView.frame), 44.0)];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        [titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
        [titleLabel setText:self.title];
        [titleView addSubview:titleLabel];
    
    
        self.navigationItem.titleView = titleView;
    
    
        contentView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [contentView setContentSize:CGSizeMake(0.0, 4000.0)];
        [contentView setDelegate:self];
        [self.view addSubview:contentView];
    
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 44.0)];
        [contentLabel setTextAlignment:NSTextAlignmentCenter];
        [contentLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
        [contentLabel setText:self.title];
        [contentView addSubview:contentLabel];
    }
    
    #pragma mark - UIScrollViewDelegate
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGPoint contentOffset = CGPointMake(0.0, MIN(scrollView.contentOffset.y + 64.0, 44.0));
        [titleView setContentOffset:contentOffset];
    }
    
    @end
    

提交回复
热议问题