Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A generic stretchy header for UITableView and UICollectionView

License

NotificationsYou must be signed in to change notification settings

gskbyte/GSKStretchyHeaderView

Repository files navigation

LicensePlatformCarthage compatibleVersion

CI StatusCoverage

Example 1Example 2Example 3Example 4Example 5

GSKStretchyHeaderView is an implementation of the stretchy header paradigm as seen on many apps, like Twitter, Spotify or airbnb. It's designed in order to accomplish the following requirements:

  • Compatibility withUITableView andUICollectionView
  • Data source and delegate independency: can be added to an existing view controller withouth interfering with your existingdelegate ordataSource
  • Provide support for frame layout, auto layout and Interface Builder.xib files
  • No need to subclass a custom view controller or to use a customUICollectionViewLayout
  • Simple usage: just implement your own subclass and add it to yourUIScrollView subclass
  • Two expansion modes: the header view can grow only when the top of the scroll view is reached, or as soon as the user scrolls down.

If you are using this library in your project, I would be more than glad toknow about it!

Usage

To add a stretchy header to your table or collection view, you just have to do this:

varstretchyHeader:GSKStretchyHeaderViewSubclass!...func viewDidLoad(){    super.viewDidLoad()letheaderSize=CGSize(width:self.tableView.frame.size.width,                             height:200) // 200 will be the default heightself.stretchyHeader=GSKStretchyHeaderViewSubclass(frame:CGRect(x:0, y:0,                                                                      width: headerSize.width,                                                                       height: headerSize.height))self.stretchyHeader.delegate=self // this is completely optionalself.tableView.addSubview(self.stretchyHeader)}

or

func viewDidLoad(){    super.viewDidLoad()letnibViews=Bundle.main.loadNibNamed("GSKTabsStretchyHeaderView", owner:self, options:nil)self.stretchyHeaderView= nibViews.firstObjectself.tableView.addSubview(self.stretchyHeaderView)}

Compatibility with iOS 11 and the iPhone X

After the changes introduced in iOS 11, some issues have appeared with the safe area adjustments to scroll views. To avoid glitches and strange behaviours, just do the following when you set up your header view:

if #available(iOS11.0,*){self.tableView.contentInsetAdjustmentBehavior=.never}

For the time being, we haven't found a better way to deal with thecontentInset adjustment, so the support for the iPhone X and thesafeAreaInsets is not there yet. This may require a major refactor of the header view and a major release. For more information, seethis issue andthis PR (#68).

Configuration

You can change multiple parameters in your stretchy header view:

// you can change wether it expands at the top or as soon as you scroll downheaderView.expansionMode=.immediate// You can change the minimum and maximum content heightsheaderView.minimumContentHeight=64 // you can replace the navigation bar with a stretchy header viewheaderView.maximumContentHeight=280// You can specify if the content expands when the table view bounces, and if it shrinks if contentView.height < maximumContentHeight. This is specially convenient if you use auto layout inside the stretchy header viewheaderView.contentShrinks=trueheaderView.contentExpands=false // useful if you want to display the refreshControl below the header view// You can specify wether the content view sticks to the top or the bottom of the header view if one of the previous properties is set to `false`// In this case, when the user bounces the scrollView, the content will keep its height and will stick to the bottom of the header viewheaderView.contentAnchor=.bottom

Creating your stretchy header

There are two ways to create your own stretchy header:

  • Create a stretchy header subclass and add subviews to itscontentView. You can layout its subviews manipulating their frames or using Auto Layout (also works withGranadaLayout:trollface:).
  • Create an Interface Builder file and map it to yourGSKStretchyHeaderView subclass. Subviews added to the stretchy header will be automatically moved to the content view, keeping their constraints. Remember to set the propertiesmaximumContentHeight andminimumContentHeight in the attributes inspector (fourth tab on the right panel in Interface Builder).

To modify the behaviour and layout of your stretchy header, just override the method-didChangeStretchFactor: in your subclass, where you can adjust it by using thestretchFactor. To get a more detailed description of the properties, please have a look at the source code. There are also a few usage examples in the example project. You can also take them as a reference for your own stretchy headers.

Example project

To run the example project, clone the repo and open the workspace fileGSKStretchyHeaderView.xcworkspace.

You can also usepod try GSKStretchyHeaderView.

Installation

GSKStretchyHeaderView is available throughCocoaPods. To install it, simply add the following line to your Podfile,you can check the Example Podfile to see how it looks like:

pod"GSKStretchyHeaderView"

GSKStretchyHeaderView is also available throughCarthage. To install it, just add this line to your `Cartfile:

github "gskbyte/GSKStretchyHeaderView"

and run

carthage update GSKStretchyHeaderView

Author

Jose Alcalá Correa

Contributions

Contributions are more than welcome! If you find a solution for a bug or have an improvement, don't hesitate toopen a pull request!

License

GSKStretchyHeaderView is available under the MIT license. See the LICENSE file for more info.

If your app usesGSKStretchyHeaderView, I'd be glad if you ping me viaTwitter or via email.

About

A generic stretchy header for UITableView and UICollectionView

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp