Swiftpack.co -  garynewby/PianoKeyboard as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Piano keyboard view for iOS, in Swift
.package(url: "https://github.com/garynewby/PianoKeyboard.git", from: "1.0.25")



An iOS piano keyboard view for iPhone or iPad, written in Swift 5. Has an IBDesignable preview in interface builder with IBInspectable properties for:

  • Number of keys
  • Hide/show note names on keys
  • Black key height and width
  • Hightlight keys, with individual colour (optional play note)
  • Custom key labels
  • Latch, toggle key on/off

All images are drawn using Core Graphics.

Example app

alt tag



To use via CocoaPods, add the following line to your Podfile:

pod 'PianoKeyboard'

Swift Package Manager

With Xcode 11+ you can add PianoKeyboard to your project using the Swift Package Manager. From the File menu select Swift Packages and then select Add Package Dependency. A dialogue then will request the package repository URL, enter:



The Example app demonstrates how to integrate and use PianoKeyboard in your project. It includes a simple AVAudioEngine/AVAudioUnitSampler based sound source.

Delegate Methods

func pianoKeyDown(_ keyNumber: Int) {
  // Called when the key corresponding to midi number keyNumber is pressed

func pianoKeyUp(_ keyNumber: Int) {
  // Called when the key corresponding to midi number keyNumber is released

Key Labels

A key's label must be assigned a value for it to show:

keyboard.setLabel(for: 60, text: "Do")
keyboard.setLabel(for: 62, text: "Re")
keyboard.setLabel(for: 64, text: "Mi")

for noteNumber in 65...72 {
    keyboard.setLabel(for: noteNumber, text: Note.name(for: noteNumber))


  • Swift 5
  • SwiftLint (for Example app) brew install swiftlint


Gary Newby


Licensed under the MIT License.


Stars: 87
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics