gpt4 book ai didi

ios - 如何将自动布局与容器转换一起使用?

转载 作者:IT王子 更新时间:2023-10-29 07:47:41 26 4
gpt4 key购买 nike

如何通过 UIViewController 容器转换方法使用自动布局:

-(void)transitionFromViewController:(UIViewController *)fromViewController
toViewController:(UIViewController *)toViewController
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;

传统上,使用 Springs/Struts,您设置初始帧(就在调用此方法之前)并在传递给该方法的动画 block 中设置最终帧。

该方法负责将 View 添加到 View 层次结构并为您运行动画。

问题是我们不能在同一位置(方法调用之前)添加初始约束,因为 View 尚未添加到 View 层次结构中。

关于如何将此方法与自动布局一起使用,我有什么想法吗?

下面是使用 Springs/Struts(框架)执行此操作的示例(感谢 cocoanetics) http://www.cocoanetics.com/2012/04/containing-viewcontrollers

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{

// XXX We can't add constraints here because the view is not yet in the view hierarchy
// animation setup
toViewController.view.frame = _containerView.bounds;
toViewController.view.autoresizingMask = _containerView.autoresizingMask;

// notify
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];

// transition
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
}

最佳答案

开始思考实用方法transitionFromViewController:toViewController:duration:options:animations:completion 无法与 Auto Layout 一起干净地工作。

现在我已经用直接调用每个“较低级别”的包含方法代替了我对这种方法的使用。它的代码有点多,但似乎提供了更大的控制权。

看起来像这样:

- (void) performTransitionFromViewController:(UIViewController*)fromVc toViewController:(UIViewController*)toVc {

[fromVc willMoveToParentViewController:nil];
[self addChildViewController:toVc];

UIView *toView = toVc.view;
UIView *fromView = fromVc.view;

[self.containerView addSubview:toView];

// TODO: set initial layout constraints here

[self.containerView layoutIfNeeded];

[UIView animateWithDuration:.25
delay:0
options:0
animations:^{

// TODO: set final layout constraints here

[self.containerView layoutIfNeeded];

} completion:^(BOOL finished) {
[toVc didMoveToParentViewController:self];
[fromView removeFromSuperview];
[fromVc removeFromParentViewController];
}];
}

关于ios - 如何将自动布局与容器转换一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20890284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com