[英]How would one change a container's content with a UIButton?
我正在嘗試將容器內的嵌入內容從tableviewcontroller更改為地圖。 我嘗試搜索它,但是所有問題都與tabview有關,而我沒有這樣做。 我將如何開始這個過程?
我也嘗試過
- (IBAction)changer:(id)sender {
UIViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
container.view = viewController1;
}
但這不起作用
嘗試
MyViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
viewController1.view.frame = self.container.bounds;
[viewController1 willMoveToParentViewController:self];
[self.container addSubview:viewController1.view];
[self addChildViewController:viewController1];
[viewController1 didMoveToParentViewController:self];
這是我在Swift中的實現:
func navigateTo(vc: NSViewController){
vc.view.frame = self.containerView.bounds;
if let currentSubview = containerView.subviews.first {
self.containerView.replaceSubview(currentSubview, with: vc.view)
} else {
self.containerView.addSubview(vc.view)
}
self.addChildViewController( vc )
}
對於動畫過渡,我實現了awakeFromNib
override func awakeFromNib() {
let animation: CATransition = CATransition()
animation.type = kCATransitionMoveIn
animation.subtype = kCATransitionFromRight
animation.duration = 0.75
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
if let _ = containerView {
containerView.wantsLayer = true
containerView.animations = NSDictionary(object: animation, forKey: "subviews") as! [String : AnyObject]
}
}
並將self.containerView.replaceSubview行替換為:
self.containerView.animator().replaceSubview(currentSubview, with: vc.view)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.