Sheehan Alam
Sheehan Alam

Reputation: 60909

How can I position my UIView to the bottom of the screen?

I have the following code to show a little notification bar right above a 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;
        }];
    }
}

The problem is, there is a gap between this UIView and a UIToolbar when I switch tabs to a UINavigationController that hides the tabbar. How can I reposition it so that it goes down?

Upvotes: 0

Views: 1469

Answers (1)

Carles Estevadeordal
Carles Estevadeordal

Reputation: 1229

If i have understod your problem correctly this should do:

What I would do is to put the positioning code for the notification bar on another function, since you also insert subviews in this piece of code. You should call it form here and in the view did load methods of this UIViewController and the UINavigationController that you call.

A trick to position correctly the the notification bar without much trouble would be to use the toolbar y position as a reference, since it is updated automatically.

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

I hope this helps.

Upvotes: 1

Related Questions