在iOS7中使用隐藏/显示标签栏动画进行Autolayout

时间:2015-06-06 21:43:49

标签: animation ios7 autolayout show-hide uitabbar

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

我找到了显示/隐藏标签栏的代码,我很满意(来源: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];

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

Before animation

这是在动画之后

After animation

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

Weird thing happen when i show it again

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

- (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;

}

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

1 个答案:

答案 0 :(得分:1)

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

----------------------。h file --------------------- < / p>

#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 file --------------------- < / p>

#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

与你的viewcontroller一样使用。

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

可能会解决您的问题。