objective-c – 什么可以阻止在动画期间遵守自动布局约束?

objective-c – 什么可以阻止在动画期间遵守自动布局约束?,第1张

概述我正试图将视图捕捉到UITabBarController的tabBar(UITabBar),从不隐藏它(Apple在tvOS App Store的’Featured’选项卡中的方式). 我可以通过在直接包含在UITabBarController中的UIViewController中设置约束来使其工作.当隐藏和显示标签栏时,视图(在我的例子中,是一个UICollectionView)完全跟随动画.但 我正试图将视图捕捉到UITabbarController的tabbar(UITabbar),从不隐藏它(Apple在tvOS App Store的’Featured’选项卡中的方式).

我可以通过在直接包含在UITabbarController中的UIVIEwController中设置约束来使其工作.当隐藏和显示标签栏时,视图(在我的例子中,是一个UICollectionVIEw)完全跟随动画.但是当我的UIVIEwController在UINavigationController中时,它也不起作用.它最终更新,但是当UITabbar动画(隐藏和显示)时,它不会更新.

以下是我使用NSLayoutConstraints设置约束的方法:
UIVIEw * targetVIEw = self.collectionVIEw;

NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:targetVIEw attribute:NSLayoutAttributetop relatedBy:NSLayoutRelationEqual toItem:self.tabbarController.tabbar attribute:NSLayoutAttributeBottom multiplIEr:1 constant:0];  NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:targetVIEw attribute:NSLayoutAttributeleft relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeleft multiplIEr:1 constant:0];  NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:targetVIEw attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeHeight multiplIEr:1 constant:0];  NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:targetVIEw attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeWIDth multiplIEr:1 constant:0];  [self.vIEw.window addConstraint:constraint1];  [self.vIEw addConstraints:@[constraint2,constraint3,constraint4]];

这是我如何用Masonry设置相同的约束(两者都有相同的结果,但这更具可读性):

[self.containerVIEw mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.wIDth.height.equalTo(self.vIEw);        make.top.equalTo(self.tabbarController.tabbar.mas_bottom);    }];

这是一个展示我的问题的示例项目:
https://www.dropbox.com/s/bxbi0gyidxhu2bz/SampleMovingWithTabBar.zip?dl=1

这是一个错误还是预期的行为?

我正试图在另一个更复杂的应用程序中实现这个“视图不隐藏在标签栏下”的行为,我得到了同样奇怪的行为.虽然在这种情况下没有涉及UINavigationController.该视图直接位于UIVIEwController(它是UITabbarController的vIEwControllers数组的一部分)中.

什么可以阻止在动画期间遵守自动布局约束?

这是我的VIEw Controller层次结构在我链接到的演示项目中的样子(UINavigationController中的VIEw Controller是在显示/隐藏标签栏时没有动画的那个):

<UITabbarController 0x7fca42d10bb0>,state: appeared,vIEw: <UILayoutContainerVIEw 0x7fca42f848b0>   | <FirstVIEwController 0x7fca42d11340>,state: disappeared,vIEw: <UIVIEw 0x7fca42f96510>   | <UINavigationController 0x7fca43812000>,vIEw: <UILayoutContainerVIEw 0x7fca42d46200>   |    | <FirstVIEwController 0x7fca42f347f0>,vIEw: <UIVIEw 0x7fca42f8bd90>
解决方法 每当您在此时使用CoreAnimation API进行动画时,布局约束在瞬态动画期间不会受到约束.

当您使用NSLayoutconstraint的属性(如常量用于动画)时,自动布局会在瞬态动画期间遵循约束.

CoreAnimations API包括UIVIEw的基于块的动画.如果您有兴趣进一步阅读,那么这里是wwdc video

但是,看看你的代码,我认为这不是你的问题.您希望在UITabbar动画时尊重布局约束.您只需要在vIEwDIDLayoutSubvIEws中设置约束而不是vIDeDIDAppear,您应该使用现有代码进行设置.

编辑—

我得到了一些关于这个问题的更多细节.首先,这不是苹果的错误. UITabbar只负责它的直接子视图控制器.它没有关于它的子结构的任何信息.你有责任将这一责任归为一类.

解决这个问题的方法是监听UIVIEwController的转换调用并相应地设置动画.

这是神奇的代码.将其添加到视图控制器.

-(voID)dIDUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator{    Nsstring *prevFocusVIEwClassname = NsstringFromClass([context.prevIoUslyFocusedVIEw class]);    Nsstring *nextFocusedVIEw = NsstringFromClass([context.nextFocusedVIEw class]);    // The tabbar is going to disappear    if ([prevFocusVIEwClassname isEqualToString:kUITabbarbuttonClassname] &&        ![nextFocusedVIEw isEqualToString:kUITabbarbuttonClassname]) {        [self.vIEw layoutIfNeeded];        [coordinator addCoordinatedAnimations:^{            [self.vIEw layoutIfNeeded];        } completion:nil];        // The tabbar is going to appear    } else if (![prevFocusVIEwClassname isEqualToString:kUITabbarbuttonClassname] &&               [nextFocusedVIEw isEqualToString:kUITabbarbuttonClassname]) {        [self.vIEw layoutIfNeeded];        [coordinator addCoordinatedAnimations:^{            [self.vIEw layoutIfNeeded];        } completion:nil];    }}

我已修复了预期输出https://www.dropbox.com/s/c8jdw367a3bnb42/SampleMovingWithTabBar-2.zip?dl=0的代码

总结

以上是内存溢出为你收集整理的objective-c – 什么可以阻止在动画期间遵守自动布局约束?全部内容,希望文章能够帮你解决objective-c – 什么可以阻止在动画期间遵守自动布局约束?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1228195.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存