iOS: 在 segue 和 popViewController 后将 UIView 返回到起始位置

3

我有一个UIViewController,在其中有一些动画。在这个UIViewController上动画的UIView具有不同的起始位置和结束位置。我还有一个segue,它将推入新的UIViewController到场景中。一切都正常工作,但是如果我返回到我的第一个UIViewController,我的UIVIew的位置会改变到起始位置。

我该如何解决这个问题?

- (void)viewDidLoad
{
   [super viewDidLoad];

     [UIView animateWithDuration:0.3  animations:^{


            self.myView.center = CGPointMake(self.myView.center.x,self.ahotherView.center.y);


        }completion:^(BOOL complete){
        }];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.myView.center = CGPointMake(self.myView.center.x, self.ahotherView.center.y);

}      

1
你在返回控制器时,在viewWillAppear、viewDidAppear、viewWillLayoutSubviews或viewDidLayoutSubviews中是否有一些视图的位置设置?因为所有这些方法都会被调用。 - AntonijoDev
@AntonijoDev 不,我还没有。我尝试在那些方法中修复这个问题,但我无法解决。 - Dima Deplov
@AntonijoDev 我其实可以修复这个问题,但我发现我的 UIView 会跳到最终位置(想要没有这个跳跃)。为了解决这个问题,我会在 - (void)viewWillLayoutSubviews 中更改代码。 - Dima Deplov
你是否使用了 popViewController 方法来返回上一页?并使用 segue 来前进? - AntonijoDev
如果您从 - (void)viewWillLayoutSubviews 中删除 self.myView.center = CGPointMake(s... 代码,会发生什么呢? - AntonijoDev
显示剩余4条评论
1个回答

5

自动布局导致视图返回初始位置。您可以关闭它或编写一些代码来覆盖约束条件。

您需要首先在Storyboard中添加约束条件,这本身就是一个完全不同的问题。我建议进行一些研究。一旦您添加了适当的约束条件并为其创建了 IBOutlet ,您将更改约束条件常量的值,然后在动画块内调用 [self.myView layoutIfNeeded] 。而不是更改 center 属性。可能看起来像这样:

//update constraint values
self.topConstraint.constant = 70;
self.bottomConstraint.constant = 20;

//animate
[UIView animateWithDuration:0.5f animations:^{
    [self.myView layoutIfNeeded];
} completion:^(BOOL finished) {

}];

尝试实现这个。 - Dima Deplov
很好的关于约束(IBOutlet)的编辑!我不知道我可以这样做。我解决了我的问题:)谢谢你 :) - Dima Deplov
回到我的原始视图控制器时,所有的UI元素都向下移动了。唯一解决这个问题的方法是在IB中关闭自动布局。感谢您的建议。 - user3344977

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接