Skip to content

Commit

Permalink
fixed layout behavior to support safe area
Browse files Browse the repository at this point in the history
  • Loading branch information
askopin committed Mar 30, 2019
1 parent ca8e9f5 commit d77b210
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
20 changes: 16 additions & 4 deletions CBFlashyTabBarController/Classes/CBFlashyTabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,25 @@ open class CBFlashyTabBar: UITabBar {
super.setItems(items, animated: animated)
reloadViews()
}


var barHeight: CGFloat = 60

override open func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = barHeight
if #available(iOS 11.0, *) {
sizeThatFits.height = sizeThatFits.height + safeAreaInsets.bottom
}
return sizeThatFits
}

open override func layoutSubviews() {
super.layoutSubviews()
let btnWidth = bounds.width / CGFloat(buttons.count)
var btnHeight = bounds.height
if #available(iOS 11.0, *) {
btnHeight -= safeAreaInsets.bottom/2.0
btnHeight -= safeAreaInsets.bottom
}

for (index, button) in buttons.enumerated() {
Expand All @@ -65,7 +77,7 @@ open class CBFlashyTabBar: UITabBar {
}
}

private func reloadViews() {
func reloadViews() {
subviews.filter { String(describing: type(of: $0)) == "UITabBarButton" }.forEach { $0.removeFromSuperview() }
buttons.forEach { $0.removeFromSuperview()}
buttons = items?.map { self.button(forItem: $0) } ?? []
Expand All @@ -75,8 +87,8 @@ open class CBFlashyTabBar: UITabBar {

private func reloadAnimations() {
buttons.forEach { (button) in
button.selectAnimation = CBTabItemSelectAnimation(duration: 0.3 / animationSpeed)
button.deselectAnimation = CBTabItemDeselectAnimation(duration: 0.3 / animationSpeed)
button.selectAnimation = CBTabItemSelectAnimation(duration: 0.5 / animationSpeed)
button.deselectAnimation = CBTabItemDeselectAnimation(duration: 0.5 / animationSpeed)
}
}

Expand Down
36 changes: 14 additions & 22 deletions CBFlashyTabBarController/Classes/CBFlashyTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,30 @@ open class CBFlashyTabBarController: UITabBarController {
super.viewDidAppear(animated)
}

private var _barHeight: CGFloat = 74
open var barHeight: CGFloat {
get {
if #available(iOS 11.0, *) {
return _barHeight + view.safeAreaInsets.bottom
} else {
return _barHeight
}
return (tabBar as? CBFlashyTabBar)?.barHeight ?? tabBar.frame.height
}
set {
_barHeight = newValue
updateTabBarFrame()
(tabBar as? CBFlashyTabBar)?.barHeight = newValue
}
}

private func updateTabBarFrame() {
var tabFrame = self.tabBar.frame
tabFrame.size.height = barHeight
tabFrame.origin.y = self.view.frame.size.height - barHeight
self.tabBar.frame = tabFrame
var tabFrame = tabBar.frame
if #available(iOS 11.0, *) {
tabFrame.size.height = barHeight + view.safeAreaInsets.bottom
} else {
tabFrame.size.height = barHeight
}
tabFrame.origin.y = self.view.frame.size.height - tabFrame.size.height
tabBar.frame = tabFrame
tabBar.setNeedsLayout()
}

open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
updateTabBarFrame()
}


@available(iOS 11.0, *)
open override func viewSafeAreaInsetsDidChange() {
if #available(iOS 11.0, *) {
super.viewSafeAreaInsetsDidChange()
}
super.viewSafeAreaInsetsDidChange()
updateTabBarFrame()
}

Expand Down
21 changes: 18 additions & 3 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="jF7-4o-3M0">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand All @@ -9,10 +9,25 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
<scene sceneID="2ac-Cy-HiD">
<objects>
<navigationController navigationBarHidden="YES" id="jF7-4o-3M0" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="5xo-O8-rVj">
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="rootViewController" id="cGK-sN-8Wd"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="uax-SA-qyq" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-1779" y="-213"/>
</scene>
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="CBFlashyTabBarController_Example" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="ExampleApp" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
Expand Down Expand Up @@ -190,7 +205,7 @@
<!--Settings-->
<scene sceneID="srN-N5-FYi">
<objects>
<viewController id="eli-5Z-mNT" customClass="SettingsViewController" customModule="CBFlashyTabBarController_Example" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="eli-5Z-mNT" customClass="SettingsViewController" customModule="ExampleApp" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Dp7-2C-esf"/>
<viewControllerLayoutGuide type="bottom" id="6Kj-bX-14V"/>
Expand Down
1 change: 1 addition & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ViewController: UIViewController {

let tabBarController = CBFlashyTabBarController()
tabBarController.viewControllers = [eventsVC, searchVC, activityVC, settingsVC]
// navigationController?.pushViewController(tabBarController, animated: true)
self.present(tabBarController, animated: true, completion: nil)
}

Expand Down

0 comments on commit d77b210

Please sign in to comment.