Decrease title while scrolling (not UITableViewController) iOS 11

I have a view controller built into the navigation controller that prefers the large headings setting to true; inside the view controller there is a scroll view.

I want the navigation bar to shrink as I scroll.

How can I archive this?

Xcode 9, Swift 4, iOS 11

+10
source share
11 answers

I didn't get it working using UIScrollView, but I archived it with other ViewControllers, using UITableView as the first view.

tableView , . , , tableView subviews .

enter image description here

, .

+42

- ? .

class P1ViewController: UIViewController, UIScrollViewDelegate
{
    var canTransitionToLarge = false
    var canTransitionToSmall = true    

    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        if canTransitionToLarge && scrollView.contentOffset.y <= 0 {
            UIView.animate(withDuration: 0.5) {
                self.navigationItem.largeTitleDisplayMode = .always
            }
            canTransitionToLarge = false
            canTransitionToSmall = true
        }
        else if canTransitionToSmall && scrollView.contentOffset.y > 0 {
            UIView.animate(withDuration: 0.5) {
                self.navigationItem.largeTitleDisplayMode = .never
            }
            canTransitionToLarge = true
            canTransitionToSmall = false
        }
    }
}
+5

prefersLargeTitles NavBar . , InterfaceBuilder, .

IB Property Inspector

,

    self.navigationController?.navigationBar.prefersLargeTitles = true
+4

. : 1- TableView ( ) . 2- FirstView FirstViewContoller.

+2

, - UIScrollView - , . , UIScrollView, WKWebView UITextView - .

+1

- Kamil Szostakowski UICollectionView.

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    UIView.animate(withDuration: 0.5, animations: {
        self.navigationController?.navigationBar.prefersLargeTitles = (velocity.y < 0)
    })
}

, .

0

dsk1306, . . WebView UIViewController .

UIWebView:

self.webView.scrollView.delegate = self

extension MyViewController: UIScrollViewDelegate {

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if #available(iOS 11.0, *) {
        UIView.animate(withDuration: 0.5, animations: {
            self.navigationController?.navigationBar.prefersLargeTitles = (velocity.y < 0)
        })
    }
}

}
0

- :

    extendedLayoutIncludesOpaqueBars = true
    scrollView.translatesAutoresizingMaskIntoConstraints = false       
    view.addSubview(scrollView)
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: view.topAnchor),
        scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        scrollView.rightAnchor.constraint(equalTo: view.rightAnchor)
        ])
0

root navigationBar VC scrollView, : UIScrollView RootVC. . UIScrollView ( 0). VC scrollViewDidScroll UIScrollView. . gif RootVC: ( , )

VC

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if #available(iOS 11.0, *) {
            let offset = scrollView.contentOffset
            rootVC.scrollView.contentOffset = offset
        }
}
0

:

  1. Pin

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView?.alwaysBounceVertical = true
  }
0

, . scrollView, .

Here's a test case for the Xcode project for links: https://github.com/DazChong/LargeTitleShrinkOnScrollView

-1
source

Source: https://habr.com/ru/post/1687350/


All Articles