Skip to content

Commit

Permalink
Add convenience initializer for floating action button with single it…
Browse files Browse the repository at this point in the history
…em (#178)
  • Loading branch information
jjochen committed Dec 17, 2018
1 parent 4e98c8f commit 14bbbd2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Example/Tests/JJFloatingActionButtonSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,5 +455,29 @@ class JJFloatingActionButtonSpec: QuickSpec {
expect(actionButton) == snapshot()
}
}

describe("JJFloatingActionButton using single item initializer") {
var actionButton: JJFloatingActionButton!
var action = "not done"

beforeEach {
actionButton = JJFloatingActionButton(image: #imageLiteral(resourceName: "Favourite"), action: { _ in
action = "done!"
})
}

it("has one item") {
expect(actionButton.items.count) == 1
}

it("has the correct icon") {
expect(actionButton.items.first?.buttonImage) == #imageLiteral(resourceName: "Favourite")
}

it("performs action when tapped") {
actionButton.sendActions(for: .touchUpInside)
expect(action).toEventually(equal("done!"))
}
}
}
}
14 changes: 14 additions & 0 deletions Sources/JJFloatingActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ import UIKit
setup()
}

/// Initializes and returns a newly allocated floating action button object with the specified image and action.
///
/// - Parameter image: The image of the action item. Default is `nil`.
/// - Parameter action: The action handler of the action item. Default is `nil`.
///
/// - Returns: An initialized floating action button object.
///
/// - SeeAlso: init(frame: CGRect)
///
@objc public convenience init(image: UIImage, action: ((JJActionItem) -> Void)? = nil) {
self.init()
addItem(title: nil, image: image, action: action)
}

internal lazy var itemContainerView: UIView = {
let view = UIView()
view.isUserInteractionEnabled = true
Expand Down

0 comments on commit 14bbbd2

Please sign in to comment.