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