View on top of UITabBar

Similar to what the Spotify or Apple Music app does when playing a song, it places a custom view on top of the UITabBar: enter image description here

The solutions I tried:

  • UITabBarController in the ViewController with the maximum container size and custom view on top of the Container View49pt above the bottom layout: Problem: Any content in the ViewControllers embedded in the UITabBarController that is bounded below is not displayed because they are hidden behind the custom layout. I tried overriding in UITabBarController, tried updating the bottom layout, Nothing. I need to resize the container view in the UITabBarController container.enter image description here size forChildContentContainer

  • # 1, UITabBar, ImageInset TabBarItem, , UITabBar. . , .

  • UITabBarController root, ViewController + : enter image description here , . , . ..

  • UITabBar UITabBarController UITabBar ( xib), UITabBar + . : , . class MyCustomTabBar : UITabBar {}, ! , myCustomTabBar self.

# 3, .

+4
4

!

enter image description here

, UITabBar, ( ), UITabBar + .

, . :

class TabBarViewController: UITabBarController {

    var currentlyPlaying: CurrentlyPlayingView!
    static let maxHeight = 100
    static let minHeight = 49
    static var tabbarHeight = maxHeight

    override func viewDidLoad() {
        super.viewDidLoad()

        currentlyPlaying = CurrentlyPlayingView(copyFrom: tabBar)
        currentlyPlaying.tabBar.delegate = self

        view.addSubview(currentlyPlaying)
        tabBar.isHidden = true
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        currentlyPlaying.tabBar.items = tabBar.items
        currentlyPlaying.tabBar.selectedItem = tabBar.selectedItem
    }
    func hideCurrentlyPlaying() {
        TabBarViewController.tabbarHeight = TabBarViewController.minHeight
        UIView.animate(withDuration: 0.5, animations: {
            self.currentlyPlaying.hideCustomView()
            self.updateSelectedViewControllerLayout()
        })
    }
    func updateSelectedViewControllerLayout() {
        tabBar.sizeToFit()
        tabBar.sizeToFit()
        currentlyPlaying.sizeToFit()
        view.setNeedsLayout()
        view.layoutIfNeeded()
        viewControllers?[self.selectedIndex].view.setNeedsLayout()
        viewControllers?[self.selectedIndex].view.layoutIfNeeded()
    }
}

extension UITabBar {

    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        var sizeThatFits = super.sizeThatFits(size)
        sizeThatFits.height = CGFloat(TabBarViewController.tabbarHeight)
        return sizeThatFits
    }
}
+4

, ( ) , . , UITabBarController, .

UITabBarController . ( ), ( , , ).

, , , , - , . < > , .

+2

UITabBar vc, App Delegate , :

ViewControllers

, App Delegate.

Delegate Singleton, . .

- , , .

() - , uid.

+1
source

This is actually very simple if you subclass UITabBarController and programmatically add a view. Using this method automatically supports changing the rotation and size of the tab bar, regardless of which version you are in.

class CustomTabBarController: UITabBarController {
  override func viewDidLoad() {
    super.viewDidLoad()

    //...do some of your custom setup work
    // add a container view above the tabBar
    let containerView = UIView()
    containerView.backgroundColor = .red
    view.addSubview(containerView)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    containerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

    // anchor your view right above the tabBar
    containerView.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true

    containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
  }
}
0
source

Source: https://habr.com/ru/post/1670495/


All Articles