如何将UIView定位到屏幕底部?

时间:2012-01-08 03:20:55

标签: iphone objective-c cocoa-touch uiview uinavigationcontroller

我有以下代码在UITabViewController

正上方显示一个小通知栏
- (void)showNotificationBar
{
    if(mentionsButton.hidden)
    {
        //DM button on the left
        [dmButton setFrame:CGRectMake(20,-2, 140, 35)];
        [directMessagesPill setFrame:CGRectMake(6, 7, 29, 20)];
        [dmCountLabel setFrame:CGRectMake(11, 5, 19, 21)];
        [directMessagesLabel setFrame:CGRectMake(43, 6, 81, 21)];

        //Mentions on the right
        [mentionsButton setFrame:CGRectMake(162,-2, 140, 35)];
        [mentionsPill setFrame:CGRectMake(161, 7, 29, 20)];
        [mentionCountLabel setFrame:CGRectMake(166, 6, 19, 21)];
        [mentionsLabel setFrame:CGRectMake(193, 6, 86, 21)];

    }
    else
    {
        //Mentions on the left
        [mentionsButton setFrame:CGRectMake(20,-2, 140, 35)];
        [mentionsPill setFrame:CGRectMake(6, 7, 29, 20)];
        [mentionCountLabel setFrame:CGRectMake(11, 5, 19, 21)];
        [mentionsLabel setFrame:CGRectMake(43, 6, 81, 21)];

        //DM on the right
        [dmButton setFrame:CGRectMake(162,-2, 140, 35)];
        [directMessagesPill setFrame:CGRectMake(161, 7, 29, 20)];
        [dmCountLabel setFrame:CGRectMake(166, 5, 19, 21)];
        [directMessagesLabel setFrame:CGRectMake(193, 6, 86, 21)];
    }

    if(!mentionsButton.hidden && !dmButton.hidden)
        notificationDivider.hidden = NO;

    if(!self.tabBarController.tabBar.hidden)
    {
        //CGRect frame = CGRectMake(0, 0, 320, 32);
        CGRect frame = CGRectMake(0, 500, 320, 32);
        //frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame) - frame.size.height;
        frame.origin.y = self.tabBarController.tabBar.frame.origin.y;
        notificationBar.frame = frame;

        //[self.navigationController.navigationBar.superview insertSubview:notificationBar belowSubview:self.navigationController.navigationBar];
        [self.tabBarController.tabBar.superview insertSubview:notificationBar belowSubview:self.tabBarController.tabBar];

        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = notificationBar.frame;
            //frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
            frame.origin.y -= frame.size.height;
            notificationBar.frame = frame;
        }];
    }
    else
    {
        CGRect frame = CGRectMake(0, 500, 320, 32);
        frame.origin.y = self.navigationController.toolbar.frame.origin.y;
        notificationBar.frame = frame;

        [self.navigationController.toolbar.superview insertSubview:notificationBar belowSubview:self.navigationController.toolbar];

        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = notificationBar.frame;
            frame.origin.y -= frame.size.height;
            notificationBar.frame = frame;
        }];
    }
}

问题是,当我将标签切换到隐藏标签栏的UIView时,此UIToolbarUINavigationController之间存在差距。如何重新定位它以使其失效?

1 个答案:

答案 0 :(得分:1)

如果我正确地解决了您的问题,应该这样做:

我要做的是将通知栏的定位代码放在另一个函数上,因为您还在这段代码中插入子视图。你应该在这里调用它,并在视图中加载了这个UIViewController的方法和你调用的UINavigationController。

正确定位通知栏没有太多麻烦的一个技巧是使用工具栏y位置作为参考,因为它会自动更新。

CGrect frame = notificationBar.frame;
frame.y =  self.navigationController.toolbar.frame.origin.y - notificationBar.frame.size.height;
notificationBar.frame = frame;

我希望这会有所帮助。