JJFloatingActionButton

@objc
@IBDesignable
public class JJFloatingActionButton : UIControl

A floating action button.

  let actionButton = JJFloatingActionButton()

  actionButton.addItem(title: "item 1", image: image1) { item in
      // do something
  }

  actionButton.addItem(title: "item 2", image: image2) { item in
      // do something
  }

  view.addSubview(actionButton)
  • The delegate object for the floating action button.

    Declaration

    Swift

    @objc
    public weak var delegate: JJFloatingActionButtonDelegate?
  • The list of action items. Default is [].

    See also

    enabledItems

    Declaration

    Swift

    @objc
    public var items: [JJActionItem] { get set }
  • The background color of the floating action button. Default is UIColor(hue: 0.31, saturation: 0.37, brightness: 0.76, alpha: 1.00).

    See also

    circleView

    Declaration

    Swift

    @IBInspectable
    public dynamic var buttonColor: UIColor { get set }
  • The background color of the floating action button with highlighted state. Default is nil.

    See also

    circleView

    Declaration

    Swift

    @IBInspectable
    public dynamic var highlightedButtonColor: UIColor? { get set }
  • The image displayed on the button by default. When only one JJActionItem is added and handleSingleActionDirectly is enabled, the image from the item is shown instead. When set to nil an image of a plus sign is used. Default is nil.

    See also

    imageView

    Declaration

    Swift

    @IBInspectable
    public dynamic var buttonImage: UIImage? { get set }
  • The size of the image view. Default is CGSize.zero. If set to .zero the actual size of the image is used.

    See also

    imageView

    Declaration

    Swift

    @objc
    public dynamic var buttonImageSize: CGSize { get set }
  • The tint color of the image view. Default is UIColor.white.

    Warning

    Only template images are colored.

    See also

    imageView

    Declaration

    Swift

    @IBInspectable
    public dynamic var buttonImageColor: UIColor { get set }
  • The default diameter of the floating action button. This is ignored if the size is defined by auto-layout. Default is 56.

    Declaration

    Swift

    @IBInspectable
    public dynamic var buttonDiameter: CGFloat { get set }
  • The size of an action item in relation to the floating action button. Default is 0.75.

    Declaration

    Swift

    @IBInspectable
    public dynamic var itemSizeRatio: CGFloat
  • The opening style of the floating action button itself. Default is JJButtonAnimationConfiguration.rotation()

    Declaration

    Swift

    public var buttonAnimationConfiguration: JJButtonAnimationConfiguration
  • The opening style of the action items. Default is JJItemAnimationConfiguration.popUp()

    Declaration

    Swift

    public var itemAnimationConfiguration: JJItemAnimationConfiguration
  • When enabled and only one action item is added, the floating action button will not open, but the action from the action item will be executed directly when the button is tapped. Also the image of the floating action button will be replaced with the one from the action item.

    Default is true.

    Declaration

    Swift

    @IBInspectable
    public var handleSingleActionDirectly: Bool { get set }
  • When enabled, the floating action button will close after an action item was tapped, otherwise the action button will stay open and has to be closed explicitly.

    Default is true.

    See also

    close

    Declaration

    Swift

    @IBInspectable
    public var closeAutomatically: Bool
  • The current state of the floating action button. Possible values are

    • .opening
    • .open
    • .closing
    • .closed

    Declaration

    Swift

    @objc
    public internal(set) var buttonState: JJFloatingActionButtonState { get }
  • The round background view of the floating action button. Read only.

    See also

    buttonColor

    Declaration

    Swift

    @objc
    public fileprivate(set) lazy var circleView: JJCircleView { get set }
  • The image view of the floating action button. Read only.

    Warning

    Setting the image of the imageView directly will not work. Use buttonImage instead.

    See also

    buttonImage

    See also

    buttonImageColor

    Declaration

    Swift

    @objc
    public fileprivate(set) lazy var imageView: UIImageView { get set }
  • The overlay view. Default background color is UIColor(white: 0, alpha: 0.5). Read only.

    Declaration

    Swift

    @objc
    public fileprivate(set) lazy var overlayView: UIControl { get set }
  • Initializes and returns a newly allocated floating action button object with the specified frame rectangle.

    See also

    init?(coder: NSCoder)

    Declaration

    Swift

    override public init(frame: CGRect)

    Parameters

    frame

    The frame rectangle for the floating action button, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This method uses the frame rectangle to set the center and bounds properties accordingly.

    Return Value

    An initialized floating action button object.

  • Returns an object initialized from data in a given unarchiver.

    Declaration

    Swift

    public required init?(coder aDecoder: NSCoder)

    Parameters

    aDecoder

    An unarchiver object.

    Return Value

    self, initialized using the data in decoder.

  • Initializes and returns a newly allocated floating action button object with the specified image and action.

    See also

    init(frame: CGRect)

    Declaration

    Swift

    @objc
    public convenience init(image: UIImage, action: ((JJActionItem) -> Void)? = nil)

    Parameters

    image

    The image of the action item. Default is nil.

    action

    The action handler of the action item. Default is nil.

    Return Value

    An initialized floating action button object.

  • Open the floating action button and show all action items.

    Remark

    Hidden items and items that have user interaction disabled are omitted.

    Declaration

    Swift

    func open(animated: Bool = true, completion: (() -> Void)? = nil)

    Parameters

    animated

    When true, button will be opened with an animation. Default is true.

    completion

    Will be handled upon completion. Default is nil.

  • Close the floating action button and hide all action items.

    Declaration

    Swift

    func close(animated: Bool = true, completion: (() -> Void)? = nil)

    Parameters

    animated

    When true, button will be close with an animation. Default is true.

    completion

    Will be handled upon completion. Default is nil.

  • Add floating action button to a given superview and place in trailing bottom corner.

    Remark

    On iOS prior to iOS 11 safeAreaInset is ignored.

    Declaration

    Swift

    func display(inView superview: UIView,
                 bottomInset: CGFloat = 16,
                 trailingInset: CGFloat = 16,
                 safeAreaInset: CGFloat = 0)

    Parameters

    superview

    The view to which the floating action button is added as a subview.

    bottomInset

    The (minimum) bottom vertical spacing in points between button and superview. Default is 16.

    trailingInset

    The (minimum) trailing horizontal spacing in points between button and superview. Default is 16.

    safeAreaInset

    The (minimum) spacing in points between button and safe area of the superview. Default is 0.

  • Add floating action button to a view of a given view controller and place in trailing bottom corner.

    Remark

    On iOS prior to iOS 11 safeAreaInset is ignored.

    Declaration

    Swift

    func display(inViewController viewController: UIViewController,
                 bottomInset: CGFloat = 16,
                 trailingInset: CGFloat = 16,
                 safeAreaInset: CGFloat = 0)

    Parameters

    viewController

    The view controller to which view the floating action button is added as a subview.

    bottomInset

    The (minimum) bottom vertical spacing in points between button and superview. Default is 16.

    trailingInset

    The (minimum) trailing horizontal spacing in points between button and superview. Default is 16.

    safeAreaInset

    The (minimum) spacing in points between button and safe area of the view controllers view. Default is 0.

Public Methods

  • Add an action item with title, image and action to the list of items. The item will be pre configured with the default values.

    Declaration

    Swift

    @discardableResult
    func addItem(title: String? = nil,
                 image: UIImage? = nil,
                 action: ((JJActionItem) -> Void)? = nil) -> JJActionItem

    Parameters

    title

    The title of the action item. Default is nil.

    image

    The image of the action item. Default is nil.

    action

    The action handler of the action item. Default is nil.

    Return Value

    The item that was added. This can be configured after it has been added.

  • Add an action item to the list of items. The item will be updated with the default configuration values.

    Declaration

    Swift

    func addItem(_ item: JJActionItem)

    Parameters

    item

    The action item.

  • Remove an action item from the list of items.

    Declaration

    Swift

    @discardableResult
    func removeItem(_ item: JJActionItem) -> JJActionItem?

    Parameters

    item

    The action item.

    Return Value

    The item that was removed. nil if item was not found.

  • Remove and returns the action item at the specified position in the list of items.

    Declaration

    Swift

    @discardableResult
    func removeItem(at index: Int) -> JJActionItem

    Parameters

    index

    The index of the action item. index must be a valid index of the list of items.

    Return Value

    The item that was removed.

  • Calls the given closure on each item that is or was added to the floating action button. Default is nil.

      let actionButton = JJFloatingActionButton()
    
      actionButton.configureDefaultItem { item in
          item.imageView.contentMode = .scaleAspectFill
    
          item.titleLabel.font = .systemFont(ofSize: 14)
    
          item.layer.shadowColor = UIColor.black.cgColor
          item.layer.shadowOpacity = 0.3
          item.layer.shadowOffset = CGSize(width: 1, height: 1)
          item.layer.shadowRadius = 0
      }
    

    Declaration

    Swift

    func configureDefaultItem(_ body: ((JJActionItem) -> Void)?)

    Parameters

    body

    A closure that takes an action item as a parameter.

  • All items that will be shown when floating action button is opened. This excludes hidden items and items that have user interaction disabled.

    Declaration

    Swift

    var enabledItems: [JJActionItem] { get }

UIControl

  • A Boolean value indicating whether the action button draws a highlight.

    Declaration

    Swift

    override open var isHighlighted: Bool { get set }

UIView

  • The natural size for the floating action button.

    Declaration

    Swift

    override open var intrinsicContentSize: CGSize { get }
  • Updates constraints for the view.

    Declaration

    Swift

    override open func updateConstraints()
  • Tells the view that its superview changed.

    Declaration

    Swift

    override public func didMoveToSuperview()
  • The shadow color of the floating action button. Default is UIColor.black.

    Declaration

    Swift

    @IBInspectable
    dynamic var shadowColor: UIColor? { get set }
  • The shadow offset of the floating action button. Default is CGSize(width: 0, height: 1).

    Declaration

    Swift

    @IBInspectable
    dynamic var shadowOffset: CGSize { get set }
  • The shadow opacity of the floating action button. Default is 0.4.

    Declaration

    Swift

    @IBInspectable
    dynamic var shadowOpacity: Float { get set }
  • The shadow radius of the floating action button. Default is 2.

    Declaration

    Swift

    @IBInspectable
    dynamic var shadowRadius: CGFloat { get set }