Skip to content

Commit

Permalink
Remove related views when button is removed from superview (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjochen authored Mar 4, 2020
1 parent e0dfa2a commit f1a126a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Example/Tests/JJFloatingActionButtonSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ class JJFloatingActionButtonSpec: QuickSpec {
}
}
}

context("and is removed from superview") {
beforeEach {
actionButton.removeFromSuperview()
}

it("removes related views from superview") {
expect(actionButton.overlayView.superview).to(beNil())
expect(actionButton.itemContainerView.superview).to(beNil())
}

it("closes") {
expect(actionButton.buttonState).toEventually(equal(.closed))
}
}
}

context("and is opened and closed") {
Expand Down Expand Up @@ -430,6 +445,21 @@ class JJFloatingActionButtonSpec: QuickSpec {
expect(actionCount).toEventually(equal(1))
}
}

context("and is removed from superview") {
beforeEach {
actionButton.removeFromSuperview()
}

it("removes related views from superview") {
expect(actionButton.overlayView.superview).to(beNil())
expect(actionButton.itemContainerView.superview).to(beNil())
}

it("closes") {
expect(actionButton.buttonState).toEventually(equal(.closed))
}
}
}

context("and is closed animated") {
Expand All @@ -445,6 +475,21 @@ class JJFloatingActionButtonSpec: QuickSpec {
it("has eventually state closed") {
expect(actionButton.buttonState).toEventually(equal(.closed))
}

context("and is removed from superview") {
beforeEach {
actionButton.removeFromSuperview()
}

it("removes related views from superview") {
expect(actionButton.overlayView.superview).to(beNil())
expect(actionButton.itemContainerView.superview).to(beNil())
}

it("closes") {
expect(actionButton.buttonState).toEventually(equal(.closed))
}
}
}

context("and is opened animated with open image") {
Expand Down
12 changes: 12 additions & 0 deletions Sources/JJFloatingActionButton+Animation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ import UIKit
}
}

internal extension JJFloatingActionButton {
func removeRelatedViewsFromSuperview() {
if overlayView.superview != nil {
overlayView.removeFromSuperview()
}

if itemContainerView.superview != nil {
itemContainerView.removeFromSuperview()
}
}
}

// MARK: - Animation State

fileprivate extension JJFloatingActionButton {
Expand Down
10 changes: 10 additions & 0 deletions Sources/JJFloatingActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ extension JJFloatingActionButton {
updateDynamicConstraints()
super.updateConstraints()
}

/// Tells the view that its superview changed.
///
public override func didMoveToSuperview() {
super.didMoveToSuperview()
if superview == nil {
close(animated: false)
removeRelatedViewsFromSuperview()
}
}
}

// MARK: - Setup
Expand Down

0 comments on commit f1a126a

Please sign in to comment.