我创建了自己的自由格式第一个 View Controller ,并在其中放置了一个 TableView 。我正确地设置了它的类并为此表创建了一个 IBOutlet。
在我的第二个 View Controller 中,我想将第一个 View Controller 的 View 显示为窗口中的幻灯片,因此我创建了 IBAction 来执行此操作:
- (IBAction)optionsButtonTapped:(id)sender {
OptionsViewController *optionsVC = [[OptionsViewController alloc] init];
// Custom CGRect:
CGFloat width = self.view.frame.size.width - 10.0;
CGFloat height = [optionsVC.optionsTableView numberOfRowsInSection: 0] * 50.0;
CGFloat x = self.view.frame.origin.x + ((self.view.frame.size.width - width) / 2);
CGFloat y = self.view.frame.origin.y + self.view.frame.size.height + (1.2 * height);
CGRect hiddenFrame = CGRectMake(x, y, width, height);
// Custom UIView:
UIView *view = optionsVC.view;
view.frame = hiddenFrame;
[self.view addSubview: view];
[self addChildViewController: optionsVC];
[optionsVC didMoveToParentViewController: self];
//NSLog(@"Frame for the view is: %@", NSStringFromCGRect(hiddenFrame));
// Animation:
[UIView animateWithDuration: 0.3 animations:^(void){
CGRect visibleFrame = hiddenFrame;
visibleFrame.origin.y = self.view.frame.origin.y + self.view.frame.size.height - 5.0 - height;
view.frame = visibleFrame;
} completion:^(BOOL finished) {
NSLog(@"Animation completed");
}];
}
我使用 NSLog 时的高度是 0,那是因为当我初始化 optionsVC(我设置了数据源和委托(delegate))时,tableview 没有被加载。即使我将高度设置为例如。 200.0 View 仍然没有出现,但是我有来自第二个 View Controller 的日志,其中执行了 viewDidLoad。
我做错了什么?
最佳答案
您应该使用 ContainerView
来显示 Controller 内部的 Controller 。您可以为其位置设置动画并使用委托(delegate)来相互通信。
也许你应该考虑你的应用程序的其他设计(请添加你如何看待你的用户界面,因为我不清楚),你应该避免使用 ContainerView
。引用Implementing a Container View Controller
关于ios - 从 UIViewController 添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481843/