Skip to content

UIScrollView sublass with ability to restrict scrolling area

License

Notifications You must be signed in to change notification settings

ealeksandrov/EARestrictedScrollView

Repository files navigation

EARestrictedScrollView

CI Status Version SwiftPM compatible Carthage compatible License Platform

DemoGIF

UIScrollView sublass with ability to restrict scrolling area.

In plain UIScrollView only contentSize can be changed, but not the origin of scrolling area. This simple and universal solution allows to restrict scrolling area with CGRect.

For objective-c projects you can use version 1.1.0.

Installation

You can setup EARestrictedScrollView using Carthage, CocoaPods, Swift Package Manager or completely manually.

Carthage

  1. Add EARestrictedScrollView to your project's Cartfile:

    github "ealeksandrov/EARestrictedScrollView"
  2. Run carthage update in your project directory.

  3. On your application targets’ “General” settings tab, in the “Linked Frameworks and Libraries” section, drag and drop EARestrictedScrollView.framework from the Carthage/Build/iOS/ folder on disk.

  4. On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script with the following contents:

    /usr/local/bin/carthage copy-frameworks

    and add the path to the framework under “Input Files”:

    $(SRCROOT)/Carthage/Build/iOS/EARestrictedScrollView.framework

CocoaPods

  1. Add EARestrictedScrollView to your project's Podfile:

    pod 'EARestrictedScrollView', '~> 2.1.0'
  2. Run pod update or pod install in your project directory.

Swift Package Manager

  1. Add EARestrictedScrollView to your project’s ‘Swift Packages’ section or Package.swift:
.package(url: "https://github.com/ealeksandrov/EARestrictedScrollView.git", from: "2.1.0")

Setting Up Manually

  1. Clone EARestrictedScrollView from Github.

  2. Copy and add EARestrictedScrollView header and implementation to your project.

  3. You can now use EARestrictedScrollView by adding the following import:

    import EARestrictedScrollView

How To Use It

Can be created from code as usual:

override func viewDidLoad() {
        super.viewDidLoad()
        
        restrictedScrollView = EARestrictedScrollView(frame: view.bounds)
        restrictedScrollView.alwaysBounceVertical = true
        restrictedScrollView.alwaysBounceHorizontal = true
        view.addSubview(restrictedScrollView)
        
        let imageView = UIImageView(image: UIImage(named: "milky-way"))
        restrictedScrollView.addSubview(imageView)
        restrictedScrollView.contentSize = imageView.frame.size
    }

Or from Interface Builder:

IB screenshot

Update scrolling area with new restrictionArea property. Reset restriction with passing CGRectZero to restrictionArea.

func flipSwitch(sender: UISwitch) {
        if sender.on {
            restrictedScrollView.restrictionArea = sender.superview!.frame
        } else {
            restrictedScrollView.restrictionArea = CGRectZero
        }
    }

To access subviews use containedSubviews property. It was added in version 0.2.0 since subviews override caused some troubles with autolayout.

let subviews = restrictedScrollView.containedSubviews

Author

Created and maintained by Evgeny Aleksandrov (@ealeksandrov).

License

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