UIViewAnimation 由属于 UINavigationController 的 UIViewController 完成?
我有一个 UINavigationController,用户可以使用它进行导航。 当将特定的 UIViewController 推送到导航堆栈时,导航栏中会出现一个“设置”按钮。当用户单击此按钮时,我想将当前视图/控制器(即屏幕上的所有内容,包括导航栏)翻转到设置视图。
所以我有一个 SettingsViewController ,我想从位于 navigationController 堆栈上的 CurrentViewController 翻转到它。
我尝试这样做时遇到了各种奇怪的行为,属于 SettingsViewController 的 UIView 将开始动画,滑动到位,导航按钮四处移动,没有任何行为像我想象的那样。
-(void)settingsHandler {
SettingViewController *settingsView = [[SettingViewController alloc] init];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.navigationController.view
cache:YES];
[self.navigationController.view addSubview:settingsView.view];
[UIView commitAnimations];
}
上述结果导致视图正确翻转,但是 SettingsViewController 的子视图都位于 (0, 0) 中,并且在转换后,它们“卡入”到位?
是因为我像这样在 viewDidLoad 中实例化并添加了子视图吗?
- (void)viewDidLoad {
UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
[imageBg setImage:[UIImage imageNamed:@"background.png"]];
[self.view addSubview:imageBg];
[imageBg release];
SettingsSubview *switchView = [[SettingsSubview alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
[self.view addSubview:switchView];
[switchView release];
[super viewDidLoad];
}
1:我应该如何正确地进行“翻转”转换,从 UINavigationController 中的 UIViewController 内到新的 UIViewController然后从新的 UIViewController 回到驻留在 UINavigationControllers 堆栈上的“原始”UIViewController?
2:在实例化子视图并将其添加到UIViewController?
-问题 2 更多的是“最佳实践”。我见过不同的方式 这样做的过程中,我很难找到或理解生命周期文档以及有关该主题的不同线程和帖子。我缺少“最佳实践”示例。
非常感谢您提供的任何帮助:)
I have an UINavigationController which the user navigates with.
When pushing a specific UIViewController onto the navigation stack, a "settings" button appear in the navigationBar. When the user clicks this button I would like to flip the current view/controller, i.e. everything on screen, including the navigationBar, over to a settings view.
So I have a SettingsViewController which I would like to flip to from my CurrentViewController that lives on a navigationController stack.
I get all kinds of strange behavior trying to do this, the UIViews belonging to the SettingsViewController will start to animate, sliding into place, the navigationButtons moves around, nothing acts as I would think.
-(void)settingsHandler {
SettingViewController *settingsView = [[SettingViewController alloc] init];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.navigationController.view
cache:YES];
[self.navigationController.view addSubview:settingsView.view];
[UIView commitAnimations];
}
The above results in the views flipping correctly, but the subviews of the SettingsViewController are all positioned in (0, 0) and after the transition, they 'snap' into place?
Is it because I instantiate and add my subviews in viewDidLoad, like this?
- (void)viewDidLoad {
UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
[imageBg setImage:[UIImage imageNamed:@"background.png"]];
[self.view addSubview:imageBg];
[imageBg release];
SettingsSubview *switchView = [[SettingsSubview alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
[self.view addSubview:switchView];
[switchView release];
[super viewDidLoad];
}
1: How should I correctly do the "flip" transition, from within the UIViewController in the UINavigationController, to a new UIViewController and subsequently from the new UIViewController and back to the "original" UIViewController residing on the UINavigationControllers stack?
2: Should I use a different approach, than the "viewDidLoad" method, when instantiating and adding subviews to a UIViewController?
-question 2 is more of a "best practice" thing. I have seen different ways
of doing it and I am having trouble either finding or understanding the life-cycle documentation and the different threads and posts on the subject. I am missing the "best practice" examples.
Thank You very much for any help given:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想以编程方式创建视图层次结构,则可以在 -loadView 中执行此操作。为此,您必须自己创建视图,添加其所有子视图,然后将其分配给视图属性,如下所示:
它有助于了解调用此方法的上下文以及默认情况下它的行为方式。第一次访问 UIViewController 的 view 属性时,默认的 getter 方法会调用 -loadView 来延迟加载视图。 -loadView 的默认实现从笔尖加载视图(如果指定了)。否则,它会创建一个普通的 UIView 对象并将其设置为控制器的视图。通过重写此方法,您可以确保视图的层次结构在第一次访问时完全形成。
-viewDidLoad 应用于视图层次结构完全加载后需要进行的任何后续设置。无论视图是从 nib 加载还是在 loadView 中以编程方式构建,都会调用此方法。
If you want to create your view hierarchy programmatically, the place to do it is in -loadView. To do so you must create the view yourself, add all of its subviews, and then assign it to the view property, like this:
It helps to understand the context in which this method gets called, and how it behaves by default. The first time the view property of a UIViewController is accessed, the default getter method calls -loadView to lazy-load the view. The default implementation of -loadView loads the view from a nib if one was specified. Otherwise it creates a plain UIView object and sets that as the controller's view. By overriding this method, you can ensure that your view's hierarchy will be fully formed the first time it is accessed.
-viewDidLoad should be used for any subsequent setup that needs to occur after the view hierarchy is fully loaded. This method will get called whether the view is loaded from a nib or constructed programmatically in loadView.