Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boyvanamstel/9510851198e5efcad0812a9835ce12c1 to your computer and use it in GitHub Desktop.
Save boyvanamstel/9510851198e5efcad0812a9835ce12c1 to your computer and use it in GitHub Desktop.
How to add views to the bouncy/elastic areas of an NSScrollView.
//
// SomeViewController.swift
//
// Created by Boy van Amstel on 15/08/16.
// Copyright © 2016 Danger Cove. All rights reserved.
//
// To change the background color, simply modify .backgroundColors: [NSColor]
import Cocoa
/// A generic view controller that contains an element that uses a scroll view, like an NSCollectionView, NSTableView etc.
class SomeViewController: NSViewController {
/// The collection view.
@IBOutlet weak var collectionView: NSCollectionView!
/// The clip view that contains the collection view.
@IBOutlet weak var clipView: NSClipView!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
// Adds a view to the top of the clip view
let topView = NSView(frame: CGRectOffset(collectionView.frame, 0, -NSHeight(collectionView.frame)))
topView.wantsLayer = true
// Sets the view's background color to red
topView.layer?.backgroundColor = NSColor.redColor().CGColor
clipView.addSubview(topView)
// Adds a view to the bottom of the clip view
// Not working correctly... Should stick to the bottom, might need auto layout
// let bottomView = NSView(frame: CGRectOffset(collectionView.frame, 0, NSHeight(collectionView.frame)))
// bottomView.wantsLayer = true
// // Sets the view's background color to red
// bottomView.layer?.backgroundColor = NSColor.redColor().CGColor
// clipView.addSubview(bottomView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment