【问题标题】:switch Container or View with different height within a scrollview在滚动视图中切换具有不同高度的容器或视图
【发布时间】:2018-10-06 22:00:11
【问题描述】:

我试图通过单击按钮在滚动视图中放置具有不同高度的不同视图。我画了一个问题的图像:

如果您单击按钮 1 (btn1),则我们会加载视图 1。如果您单击按钮 2,则我们会加载视图 2。

在按钮上方和显示视图下方都有小部件。所以显示视图需要以某种方式具有动态高度,不会影响滚动视图中的其他元素。我尝试了 container_views,但无法连接不同高度的视图控制器。

更新:(这里是另一个例子)

【问题讨论】:

  • 如果视图 1 是 200 像素,视图 2 是 400 像素,“显示视图需要以某种方式具有动态高度,不会影响滚动视图内的其他元素”是什么意思,两个视图是否应该具有相同的高度,或者您的意思是使用视图高度,并且一旦您插入的视图结束,视图下方的所有小部件都会跟随...
  • 我想要不同高度的视图。所以如果 view1 = 200px 和 view2 = 400px。如何在我写“一些容器或视图”的框中显示它。此框下方的任何内容都应自动向下或向上推以适应不同高度的视图。
  • 那我画两个例子
  • @Ladislav 现在怎么样?是不是更清楚了?
  • 是的,现在很清楚了:) 你使用自动布局吗?

标签: ios view height


【解决方案1】:

这就是我解决这个问题的方法,UIScrollView 中的views 之一就是我所说的containerView,例如,我的ViewController 中有containerView 的出口。这是我的层次结构在测试应用程序中的样子:

如您所见,我目前在 containerView 中有一个子视图,它定义了一个高度,橙色的 BottomView - 具有为 containerView 定义的垂直间距约束。

当用户点击替换视图(在我的示例中为replaceView(_) 方法)时,您只需首先从containerView 中删除所有子视图,然后添加新视图,因为我们使用自动布局,橙色视图将始终位于下方容器视图无论容器视图有多大/多小...

@IBAction func replaceView(_ sender: UIButton) {
    //remove all subviews from container view to be replaced
    for subview in containerView.subviews {
        subview.removeFromSuperview()
    }

    let greenView = UIView()
    greenView.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(greenView)

    NSLayoutConstraint.activate([
        greenView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
        greenView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
        greenView.topAnchor.constraint(equalTo: containerView.topAnchor),
        greenView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
        greenView.heightAnchor.constraint(equalToConstant: 50)
        ])

    greenView.backgroundColor = .green
}

如果您想在另一个 Storyboard View Controller 中添加一个视图,您可以像这样添加它:

我假设您的 Storyboard 被称为 Main 并且我添加的视图控制器的 Storyboard ID 是 GreenStoryboardID

    @IBAction func replaceView(_ sender: UIButton) {
    //remove all subviews from container view to be replaced
    for subview in containerView.subviews {
        subview.removeFromSuperview()
    }

    let storyboard = UIStoryboard(name: "Main", bundle:nil)

    let greenViewController = storyboard.instantiateViewController(withIdentifier: "GreenStoryboardID")
    guard let greenView = greenViewController.view else { fatalError() }
    //add view properly so it is uses UIViewController contaniment methods
    addChildViewController(greenViewController)
    containerView.addSubview(greenView)
    greenViewController.didMove(toParentViewController: self)

    greenView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        greenView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
        greenView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
        greenView.topAnchor.constraint(equalTo: containerView.topAnchor),
        greenView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
        greenView.heightAnchor.constraint(equalToConstant: 50)
        ])
}

这取决于你如何正确调整视图控制器的大小,这里我只是将它的高度设置为 50,但是你可以在 UIViewController 子类中有一个方法,例如它可以让你知道有多大视图必须适合所有内容到容器视图中

【讨论】:

  • 那么我不能使用情节提要吗?我有复杂的东西进入我必须在它们之间切换的视图。不能按语法来做。除非我可以在 .xib 文件之间切换,否则也可以!
  • 不确定你的意思是你不能使用情节提要,如果你的每个视图都是情节提要中自己的视图控制器,你可以做到...\
  • 那么如果我将视图放入视图控制器中,您将如何将视图控制器放入容器视图中?它们都有不同的高度。
  • 更新了答案,说明如何从另一个 Storyboard View Controller 获取视图并使用 UIViewController 包含将其添加到容器视图中
  • 我已经实现了你的故事板版本。唯一的问题是视图高度不起作用。即使我在约束中设置了高度,它也保持相同的高度。但它可以很好地交换视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
相关资源
最近更新 更多