ios容器视图更改视图

时间:2018-08-29 21:55:32

标签: ios swift

我有一个containerView添加到了View Controller。我希望在轻扫containerView时更改容器中的视图。但是,当我在滑动动作功能中使用以下命令时,它会将视图添加到整个页面,而不仅仅是更改容器内的视图。

 class SwipeDateViewController: UIViewController, UIGestureRecognizerDelegate {
    @IBOutlet weak var swipeContainer: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func swipeLeftHandler(_ sender: UISwipeGestureRecognizer) {
        let viewController = self.storyboard!.instantiateViewController(withIdentifier: "swipeViewControllerStoryboard") as! SwipeViewController
        self.swipeContainer.addSubview(viewController.view)
    }
}

如何仅更改容器中的视图而不更新整个屏幕?

1 个答案:

答案 0 :(得分:1)

我认为也许您可以在自定义vc中添加修改功能。 然后只需运行它的功能即可。

例如:

var customVC:EmbeddedViewController?

func addView() {
    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "EmbeddedViewController") as! EmbeddedViewController
    self.addChild(vc)
    vc.view.bounds = CGRect.init(x: 20, y: 40, width: 50, height: 60)
    self.view.addSubview(vc.view)
    vc.didMove(toParent: self)
    customVC = vc
}


@IBAction func actionAddView(_ sender: Any) {
  customVC?.changeColor(color: UIColor.black)
}

EmbeddedViewController

class EmbeddedViewController: UIViewController {
  public func changeColor(color:UIColor) {
      self.view.backgroundColor = color
  }
}