【问题标题】:Autolayout with hide/show tab bar animation in iOS7iOS7中带有隐藏/显示标签栏动画的自动布局
【发布时间】:2015-06-06 21:43:48
【问题描述】:

我正在尝试实现显示/隐藏标签栏效果,内容将被扩展以填充标签栏曾经所在的空间。

我找到了显示/隐藏标签栏的代码,我很满意(来源:http://www.developers-life.com/hide-uitabbarcontrolleruitabbar-with-animation.html

我添加了以下代码来相应地定位我的按钮:

    if (hiddenTabBar) {

    self.constraintToBottom.constant=0;
    [self.TestButton setNeedsUpdateConstraints];
} else {
    self.constraintToBottom.constant=-49;
    [self.TestButton setNeedsUpdateConstraints];

}
[self.TestButton layoutIfNeeded];

它按预期工作。除了按钮的动画。这是动画前应用的初始画面:

这是在动画之后

我可以成功隐藏标签栏并使用正确的动画定位按钮。但是,当我想再次显示选项卡栏时,按钮似乎从屏幕的底部(外部)开始,而不是第二张图中所示的位置。我已经调整了动画时间,以便在制作动画时可以捕捉屏幕:

以下是我隐藏/显示标签栏操作的完整代码

- (IBAction)TestTapped:(id)sender {
[UIView beginAnimations:nil context:NULL];
if(hiddenTabBar)
    [UIView setAnimationDuration:60];
else
    [UIView setAnimationDuration:0.5];
for(UIView *view in self.tabBarController.view.subviews)
{
    CGRect _rect = view.frame;
    if([view isKindOfClass:[UITabBar class]])
    {
        if (hiddenTabBar) {

            _rect.origin.y = 431;
            [view setFrame:_rect];


        } else {
            _rect.origin.y = 480;
            [view setFrame:_rect];

        }
    } else if(view==self.TestButton)
    {
        NSLog(@"ZZ");
    }
    else{
        if (hiddenTabBar) {

            _rect.size.height = 431;
            [view setFrame:_rect];
        } else {
            _rect.size.height = 480;
            [view setFrame:_rect];

        }
    }

}
if (hiddenTabBar) {

    self.constraintToBottom.constant=0;
    [self.TestButton setNeedsUpdateConstraints];
} else {
    self.constraintToBottom.constant=-49;
    [self.TestButton setNeedsUpdateConstraints];

}
[self.TestButton layoutIfNeeded];
[UIView commitAnimations];
hiddenTabBar =!hiddenTabBar;

}

我希望按钮的动画从我的第二个图表的确切位置开始。

【问题讨论】:

    标签: animation ios7 autolayout show-hide uitabbar


    【解决方案1】:

    您可以使用此类别来隐藏/显示带有动画的标签栏。

    ----------------------.h 文件----------

    #define screenSize ([[UIScreen mainScreen ] bounds])
    @interface UITabBarController (HideTabBar)
    - (void)hideTabBarAnimated:(BOOL)animated;
    - (void)showTabBarAnimated:(BOOL)animated;
    - (void)hideTabBarAnimated:(BOOL)animated complition:(void(^)())complition;
    
    @end
    

    ----------.m 文件----------

    #define kAnimationDuration .2
    @implementation UITabBarController (HideTabBar)
    
    - (void)hideTabBarAnimated:(BOOL)animated complition:(void(^)())complition
    {
        CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect tabBarControllerFrame = self.view.frame;
        if (statusbarFrame.size.height>20)
        {
            tabBarControllerFrame.size.height =  screenSize.size.height + self.tabBar.frame.size.height - 20.0;
        }
        else
        {
            tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height ;
        }
    
        if (animated) {
            [UIView animateWithDuration:0.2 animations:^{
                [self.view setFrame:tabBarControllerFrame];
            } completion:^(BOOL finished) {
                if (complition) {
                    complition();
                }
            }];
        }
        else
        {
            [self.view setFrame:tabBarControllerFrame];
            if (complition) {
                complition();
            }
        }
    }
    - (void)hideTabBarAnimated:(BOOL)animated
    {
        [self hideTabBarAnimated:animated complition:nil];
    }
    
    - (void)showTabBarAnimated:(BOOL)animated {
        CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect tabBarControllerFrame = self.view.frame;
        if (statusbarFrame.size.height>20)
        {
            tabBarControllerFrame.size.height =  screenSize.size.height - 20.0;
        }
        else
        {
            tabBarControllerFrame.size.height = screenSize.size.height ;
        }
    
        if (animated) {
            [UIView animateWithDuration:0.2 animations:^{
                [self.view setFrame:tabBarControllerFrame];
            } completion:^(BOOL finished) {
    
            }];
        }
        else
            [self.view setFrame:tabBarControllerFrame];
    }
    @end
    

    与您的视图控制器一样使用。

    [self.tabBarController hideTabBarAnimated:YES];//You can use completion handler if you want.
    

    也许它会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多