Skip to content

Commit

Permalink
Make the default button size configurable (#188)
Browse files Browse the repository at this point in the history
Make default button size configurable
  • Loading branch information
jjochen committed Feb 1, 2019
1 parent fee5841 commit ff110f7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ internal class ConfigurationExampleViewController: UIViewController {
view.addSubview(actionButton)

actionButton.translatesAutoresizingMaskIntoConstraints = false
actionButton.widthAnchor.constraint(equalToConstant: 65).isActive = true
actionButton.heightAnchor.constraint(equalToConstant: 65).isActive = true
if #available(iOS 11.0, *) {
actionButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16).isActive = true
actionButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16).isActive = true
Expand All @@ -48,6 +46,7 @@ internal class ConfigurationExampleViewController: UIViewController {
}

fileprivate func configureActionButton() {
actionButton.buttonDiameter = 65
actionButton.overlayView.backgroundColor = UIColor(hue: 0.31, saturation: 0.37, brightness: 0.10, alpha: 0.30)
actionButton.buttonImage = #imageLiteral(resourceName: "Dots")
actionButton.buttonColor = .red
Expand Down
28 changes: 28 additions & 0 deletions Example/Tests/JJFloatingActionButtonSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -479,5 +479,33 @@ class JJFloatingActionButtonSpec: QuickSpec {
expect(action).toEventually(equal("done!"))
}
}

describe("JJFloatingActionButton using layout constraints") {
let superviewFrame = CGRect(origin: .zero, size: CGSize(width: 200, height: 300))

var actionButton: JJFloatingActionButton!
var superview: UIView!

beforeEach {
superview = UIView(frame: superviewFrame)
superview.backgroundColor = .white

actionButton = JJFloatingActionButton()
superview.addSubview(actionButton)

actionButton.translatesAutoresizingMaskIntoConstraints = false
actionButton.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 16).isActive = true
actionButton.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -16).isActive = true
}

it("looks correct") {
expect(superview) == snapshot()
}

it("looks correct when diameter is set") {
actionButton.buttonDiameter = 100
expect(superview) == snapshot()
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion Sources/JJFloatingActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ import UIKit
}
}

/// The default diameter of the floating action button.
/// This is ignored if the size is defined by autolayout.
/// Default is `56`.
///
@objc @IBInspectable public dynamic var buttonDiameter: CGFloat = 56 {
didSet {
invalidateIntrinsicContentSize()
}
}

/// The size of an action item in relation to the floating action button.
/// Default is `0.75`.
///
Expand Down Expand Up @@ -356,7 +366,7 @@ extension JJFloatingActionButton {
/// The natural size for the floating action button.
///
open override var intrinsicContentSize: CGSize {
return CGSize(width: 56, height: 56)
return CGSize(width: buttonDiameter, height: buttonDiameter)
}
}

Expand Down

0 comments on commit ff110f7

Please sign in to comment.