我有一个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)
}
}
如何仅更改容器中的视图而不更新整个屏幕?
答案 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
}
}