Search code examples
swift3cosmicmind

Bottom View does not show in ToolbarController swift 3


I'm using ToolbarController of Material Framework by cosmicmind in swift 3. I have two UIViews in this controller, One is at top and other at bottom of screen. Bottom UIView does not show in Controller. I think it move down the view beyond screen.

This issue come when tested with real device. Else simulator shows correct behavior.

import UIKit
import Material

class RootViewController: UIViewController {
  open override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = Color.white
    prepareToolbar()
    handleTopView()
    handleBottomView()
  }

  func handleTopView() {
    let topView = UIView()
    topView.frame = CGRect(x: 0.00, y: 0.00, width: view.frame.size.width, height: 40.00)
    topView.backgroundColor = UIColor.gray
    view.addSubview(topView)
  }

  func handleBottomView() {
    let bottomView = UIView()
    bottomView.frame = CGRect(x: 0.00, y: self.view.frame.size.height, width: view.frame.size.width, height: 40.00)
    bottomView.backgroundColor = UIColor.blue
    view.addSubview(bottomView)
  }
}

extension RootViewController {
    fileprivate func prepareToolbar() {
      guard let toolbar = toolbarController?.toolbar else {
        return
      }

      toolbar.title = "Material"
      toolbar.titleLabel.textColor = .white
      toolbar.titleLabel.textAlignment = .left

      toolbar.detail = "Build Beautiful Software"
      toolbar.detailLabel.textColor = .white
      toolbar.detailLabel.textAlignment = .left
    }
 }

Solution

  • The issue is that you are doing frame calculations in the viewDidLoad function and those calculations are not considering the ToolbarController calculations because at the point of construction for the ToolbarController the RootViewController is not connected, it is passed in as a parameter, which is after the viewDidLoad method is called. You can use the Layout API in Material to dynamically calculate your view's position, for example the updated code would look like:

    import UIKit
    import Material
    
    class RootViewController: UIViewController {
        open override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = Color.white
            prepareToolbar()
            handleTopView()
            handleBottomView()
        }
    
        func handleTopView() {
            let topView = UIView()
            topView.backgroundColor = UIColor.gray
            view.layout(topView).top().horizontally().height(40)
        }
    
        func handleBottomView() {
            let bottomView = UIView()
            bottomView.backgroundColor = UIColor.blue
            view.layout(bottomView).bottom().horizontally().height(40)
        }
    }
    
    extension RootViewController {
        fileprivate func prepareToolbar() {
            guard let toolbar = toolbarController?.toolbar else {
                return
            }
    
            toolbar.title = "Material"
            toolbar.titleLabel.textColor = .white
            toolbar.titleLabel.textAlignment = .left
    
            toolbar.detail = "Build Beautiful Software"
            toolbar.detailLabel.textColor = .white
            toolbar.detailLabel.textAlignment = .left
        }
    }
    

    I tested that this works with the latest ToolbarController sample project.

    All the best :)