【问题标题】:StackView insertion offset issueStackView 插入偏移问题
【发布时间】:2017-04-30 09:11:44
【问题描述】:

我正在尝试将 UIViewController 视图动态插入到水平 UIStackView 中。 UIStackView 嵌入在 UIScrollView 中。

如果我尝试使用下面代码中注释的相同方法在堆栈视图中添加标签或按钮,一切正常。但是当我尝试插入视图控制器的视图时,它无法正确布局组件。

这是我的布局:

这是我的 ViewController 代码:

import UIKit

class ViewController: UIViewController {
  @IBOutlet weak var stackview: UIStackView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  @IBAction func addComponents(_ sender: Any) {
    let componentsView = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ComponentsView")
    //componentsView.preferredContentSize = CGSize(width: 375, height: 200)
    //let button = UIButton(type: .system)
    //button.setTitle("Test", for: .normal)
    stackview.insertArrangedSubview(componentsView.view, at: 1)
    UIView.animate(withDuration: 0.5) {
      self.stackview.layoutIfNeeded()
    }
  }
}

结果如下:

在构建和执行期间都没有警告。

我错过了什么吗?

【问题讨论】:

    标签: ios swift layout uiviewcontroller uistackview


    【解决方案1】:

    添加我对堆栈视图的观察,不知道它是否适用于这种情况。

    如果我们使用故事板中的堆栈视图并在其中添加一些视图,那么我们需要将高度作为必填字段。 (填充 - 填充属性)。

    尝试在添加之前先给出子视图的高度。它会按预期工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多