Skip to content

Commit

Permalink
Clean up code for safeAreaInsets update
Browse files Browse the repository at this point in the history
  • Loading branch information
scenee committed Oct 22, 2018
1 parent c28ab32 commit 03966f3
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions Framework/Sources/FloatingPanelController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
public var contentInsetAdjustmentBehavior: ContentInsetAdjustmentBehavior = .always

private var floatingPanel: FloatingPanel!
private var layoutInsetsObserves: [NSKeyValueObservation] = []

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Expand Down Expand Up @@ -140,12 +139,12 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI

public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 11.0, *) {
// Do nothing
} else {
if let parent = parent {
self.update(safeAreaInsets: parent.layoutInsets)
}

// I needs to update safeAreaInsets here to ensure that the `adjustedContentInsets` has a correct value.
// Because the parent VC does not call viewSafeAreaInsetsDidChange() expectedly and
// `view.safeAreaInsets` has a correct value of the bottom inset here.
if let parent = parent {
self.update(safeAreaInsets: parent.layoutInsets)
}
}

Expand Down Expand Up @@ -198,21 +197,6 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI

parent.addChild(self)

layoutInsetsObserves.removeAll()

// Must track safeAreaInsets/{top,bottom}LayoutGuide of the `parent.view` to update floatingPanel.safeAreaInsets`.
// Because the parent VC does not call viewSafeAreaInsetsDidChange() expectedly on the bottom inset's update.
// So I needs to observe them. It ensures that the `adjustedContentInsets` has a correct value.
if #available(iOS 11.0, *) {
let observe = parent.observe(\.view.safeAreaInsets) { [weak self] (vc, chaneg) in
guard let self = self else { return }
self.update(safeAreaInsets: vc.layoutInsets)
}
layoutInsetsObserves.append(observe)
} else {
// KVOs for topLayoutGuide & bottomLayoutGuide are not effective. Instead, safeAreaInsets will be updated in viewDidAppear()
}

// Must set a layout again here because `self.traitCollection` is applied correctly on it's added to a parent VC
floatingPanel.layoutAdapter.layout = fetchLayout(for: traitCollection)
floatingPanel.layoutViews(in: parent)
Expand All @@ -232,8 +216,6 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
return
}

layoutInsetsObserves.removeAll()

floatingPanel.dismiss(animated: animated) { [weak self] in
guard let self = self else { return }

Expand Down

0 comments on commit 03966f3

Please sign in to comment.