【问题标题】:Bottom View does not show in ToolbarController swift 3底视图不显示在 ToolbarController swift 3
【发布时间】:2017-03-01 14:53:20
【问题描述】:

我在 swift 3 中通过 cosmicmind 使用 ToolbarControllerMaterial Framework。我在这个控制器中有两个 UIView,一个在屏幕顶部,另一个在屏幕底部。底部 UIView 不显示在控制器中。我认为它会将视图向下移动到屏幕之外。

使用真机测试时会出现此问题。否则模拟器显示正确的行为。

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
    }
 }

【问题讨论】:

    标签: swift3 cosmicmind


    【解决方案1】:

    问题是您在 viewDidLoad 函数中进行帧计算,而这些计算没有考虑 ToolbarController 计算,因为在构造 ToolbarController 时,RootViewController 没有连接,它是作为参数传入,在调用viewDidLoad 方法之后。您可以使用Material 中的Layout API 来动态计算视图的位置,例如更新后的代码如下所示:

    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
        }
    }
    

    我测试过这适用于最新的ToolbarController sample project

    一切顺利:)

    【讨论】:

    • 我尝试将 y 原点减少为 40,但现在不起作用。我逐渐减少到 100。现在用这个代码显示:bottomView.frame = CGRect(x: 0.00, y: self.view.frame.size.height - 100, width: view.frame.size.width, height: 40.00)
    • 我可以看看你是如何初始化工具栏的吗?
    • 我正在使用来自 GitHub 的 ToolbarController Sample Project 的最新示例项目。除了我在上面的问题中提到的 RootViewController 之外,代码完全相同。在此我只添加两个视图。一个在顶部,另一个在底部。
    • 我更新了对您问题的回答。让我知道这是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多