Twitter-Like Scrolling Title

后端 未结 3 1654
离开以前
离开以前 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:11

    Thanks for the Code Ryan, It works fine in my project. The code has a little mistakes but I fixed it and here is the version in Swift in case anybody need it.

        self.title = "My Title"
        titleView = UIScrollView(frame: CGRectMake(0.0, 0.0, 100.0, 44.0))
        titleView.contentSize = CGSizeMake(0.0, 88.0)
        self.view.addSubview(titleView)
    
        var titleLabel:UILabel = UILabel(frame: CGRectMake(0.0, 44.0, CGRectGetWidth(titleView.frame), 44.0))
        titleLabel.textAlignment = NSTextAlignment.Center
        titleLabel.font = UIFont(name: "HelveticaNeue-UltraLight", size: 17)
        titleLabel.text = self.title
        titleView.addSubview(titleLabel)
    
        self.navigationItem.titleView = titleView
    
        contentView = UIScrollView(frame: self.view.bounds)
        contentView.contentSize = CGSizeMake(0.0, 4000.0)
        contentView.delegate = self
        self.view.addSubview(contentView)
    
        var contentLabel:UILabel = UILabel(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 44.0))
        contentLabel.textAlignment = NSTextAlignment.Center
        contentLabel.font = UIFont(name: "HelveticaNeue-UltraLight", size: 17)
        contentLabel.text = self.title
        contentView.addSubview(contentLabel)
    
    override func scrollViewDidScroll(scrollView: UIScrollView){
    
        var contentnOffset:CGPoint = CGPointMake(0.0, min(scrollView.contentOffset.y + 64.0, 44.0))
        titleView.setContentOffset(contentnOffset, animated: true)
    
    }
    

提交回复
热议问题