Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience initializer for floating action button with single item #178

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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