如何将UIView滑动到UITabBar的正上方



我有一个UIView,我想从一个UITabBar后面滑到它的正上方。

这行不通。我的视图没有显示。

- (void)showNotificationBar
{   
    CGRect frame = CGRectMake(0, 500, 320, 32);
    frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame) - frame.size.height;
    notificationBar.frame = frame;
    [self.parentViewController.tabBarController.tabBar.superview insertSubview:notificationBar 
                                                                  belowSubview:self.parentViewController.tabBarController.tabBar];
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = notificationBar.frame;
        frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame);
        notificationBar.frame = frame;
    }];
}

像这样初始化frame.origin.y:

frame.origin.y = self.tabBarController.tabBar.frame.origin.y;

在动画块中,像这样设置:

frame.origin.y -= frame.size.height;

如果你想在每个视图中显示它,你可以这样做:要么在每个视图的底部显示它,要么在应用程序的窗口中显示它。我个人更喜欢第二种方法,因为它有助于避免重复代码:

CGFloat notificationBarHeight = 40.0f;
UIView *notificationBar = [[UILabel alloc]initWithFrame:CGRectMake(0, self.tabBarController.tabBar.frame.origin.y - notificationBarHeight, 320, notificationBarHeight)];
[self.window insertSubview:notificationBar atIndex:[[self.window subviews]count]];

[self。view insertSubview:notificationView atIndex:1];

最新更新