如何在Swift中以编程方式用自定义视图替换IBOutlet UIView?

3

在Storyboard中,它将一个名为replacementView的视图替换为UIView

@IBOutlet var replacementView: UIView!

已连接。

我想用另一个视图替换replacementView

replacementView = SecondViewController().view

但是它不起作用。问题出在哪里?

你想要实现什么?你检查了容器视图控制器吗?还要检查 addSubView: 方法。 - Midhun MP
1
请定义“不起作用”。您期望什么,实际得到了什么? - Phillip Mills
@Midhun MP,我想用动画提示不同的按钮并更改不同的子视图。 - Chao
@Phillip Mills,不起作用是因为它没有显示我分配的视图。 - Chao
也许 SecondViewController 没有被实例化? - Pranav Wadhwa
2个回答

1

replacementView只是一个引用。你需要改变视图堆栈中的对象。

你应该保留对replacementView的父级的引用。接下来,从parentView中删除replacementView,并将SecondViewController().view添加到parentView中。

我建议您尝试将您的SecondViewController().view作为replacementView添加,并添加填充约束。

您还应该记住保留SecondViewController,否则它可能在出现之前被释放。请阅读有关addChildViewController(childController: UIViewController) UIViewController方法的内容。


-1

基本上,您需要向要添加的视图添加约束,然后删除旧视图。假设项目在父视图中按顺序排列,以下是一些可以为您执行此操作的代码。

func replaceView(oldView:UIView, newView: UIView, spacingBetweenViews: CGFloat) {
    //loop through, find the view, and add a constraint between the next and previous items.
    let parentView = oldView.superview ?? UIView()
    for (index, subview) in parentView.subviews.enumerated() {
        let PREVIOUS_VIEW = index - 1
        let NEXT_VIEW = index + 1
        if subview == oldView {
            if index == 0 {
                //if the first view
                let constraints = [
                    parentView.topAnchor.constraint(
                        equalTo: newView.topAnchor,
                        constant: spacingBetweenViews),
                    newView.bottomAnchor.constraint(
                        equalTo: parentView.subviews[NEXT_VIEW].topAnchor,
                        constant: spacingBetweenViews)
                ]
                NSLayoutConstraint.activate(constraints)
            } else if index == parentView.subviews.count - 1 {
                // if the last view
                let constraints = [
                    parentView.subviews[PREVIOUS_VIEW].bottomAnchor.constraint(
                        equalTo: newView.topAnchor,
                        constant: spacingBetweenViews),
                    newView.bottomAnchor.constraint(
                        equalTo: parentView.bottomAnchor,
                        constant: spacingBetweenViews)
                ]
                NSLayoutConstraint.activate(constraints)
            } else {
                let constraints = [
                    parentView.subviews[PREVIOUS_VIEW].bottomAnchor.constraint(
                        equalTo: newView.topAnchor,
                        constant: spacingBetweenViews),
                    newView.bottomAnchor.constraint(
                        equalTo: parentView.subviews[NEXT_VIEW].topAnchor,
                        constant: spacingBetweenViews)
                ]
                NSLayoutConstraint.activate(constraints)
            }
            parentView.subviews[index].removeFromSuperview()
        }
    }
}

将子视图移动到不同的视图中是完全没有意义的任务。 - Sulthan
@Sulthan 你是什么意思?这段代码并没有移动子视图,而是替换了一个,就像原始问题所问的那样。 - Developer Extraordinare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接