简体   繁体   中英

How can i replace a IBOutlet uiview programmatically with a custom view in swift

it has a replacementView as UIView in a storyboard with

@IBOutlet var replacementView: UIView!

connected.

I want to replace the replacementView with

replacementView = SecondViewController().view

But it doesn't work. What is the problem?

replacementView is just reference. You have to change object in view stack.

You should keep reference to replacementView 's parent. Next remove replacementView from parentView and add SecondViewController().view to parentView.

I suggest you to try add your SecondViewController().view as replacementView and add fill constraints.

You should also remember about retain SecondViewController, otherwise it could be dealocated before it will appear. Read about addChildViewController(childController: UIViewController) UIViewController method.

Essentially you need to add constraints to the view you are adding and then remove the old view. Assuming the items are in order in your parent view, here's some code that would do that for you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM