3

当应用程序连接到服务器时,我会出现一条小消息。它在应用程序启动时运行良好,以及应用程序需要显示此消息的其余时间,但如果我让应用程序休眠,我将其置于后台,然后将其唤醒,消息再次出现,但整个框架向上移动,请参见屏幕截图。第一个是在启动后,底部的消息出现并且正确地消失,并且视图恢复到过去的样子,完美。

在此处输入图像描述

但是第二个屏幕截图是让应用程序进入后台,然后在“”上唤醒它的结果,applicationDidBecomeActive我有一个方法需要连接到服务器,因此必须出现小视图。问题是窗口或navigation controller状态栏后面向上移动。并留下那个空白

应用程序从启动

这是我用来缩短导航视图并添加该消息视图并使其再次变长的代码。

- (void)addMessageView:(NSString *)message{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if(![window viewWithTag:3]){//Si no hay un messageView no lo meto.
    [window addSubview:messageView];
    CGRect frameWindow = window.frame;
    int heightMessage = 50;
    messageView.frame = CGRectMake(0, frameWindow.size.height, 320, heightMessage);



    UINavigationController *nav = window.rootViewController;


    //Pillo el UILabel y lo seteo
    UILabel *label = (UILabel *)[messageView viewWithTag:1];
    label.text = message;
    CGSize maximumLabelSize = CGSizeMake(296,9999);
    CGSize sizeMsg = [label.text sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];
    UIActivityIndicatorView *activity = (UIActivityIndicatorView *)[messageView viewWithTag:2];
    int xLabel = (320 - (sizeMsg.width + activity.frame.size.width + 5))/2;

    CGRect frameActivity = activity.frame;
    activity.frame = CGRectMake(xLabel, 13, frameActivity.size.width, frameActivity.size.height);
    label.frame = CGRectMake(xLabel + activity.frame.size.width + 5, 15, sizeMsg.width, sizeMsg.height);


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];


    nav.view.frame = CGRectMake(frameWindow.origin.x, frameWindow.origin.y, frameWindow.size.width, frameWindow.size.height - heightMessage);
    messageView.frame = CGRectMake(0, frameWindow.size.height - heightMessage, 320, heightMessage);

    [UIView commitAnimations];

}
}

并且还把它放回以前的位置: - (void)hideMessageView{

UIWindow *window = [UIApplication sharedApplication].keyWindow;
UINavigationController *nav = window.rootViewController;
CGRect frameWindow = window.frame;
if([window viewWithTag:3]){//Si no hay un messageView no lo meto.
    [UIView animateWithDuration:0.3
                     animations:^{
                         nav.view.frame = CGRectMake(frameWindow.origin.x, frameWindow.origin.y, frameWindow.size.width, frameWindow.size.height);
                         messageView.frame = CGRectMake(0, frameWindow.size.height +5 , 320, 0);
                     }
                     completion:^(BOOL finished){
                         [messageView removeFromSuperview];

                     }];
}
NSLog(@"Tam Window: %@ y nav %@", frameWindow, nav.view.frame);
}

让我知道你的想法。

4

1 回答 1

0

尝试禁用Autolayout,我的 UI 也有同样的问题。

于 2013-02-08T15:36:27.173 回答