如果与后端服务器没有连接,我正在尝试实施“通知”。
现在,我有一个ExceptionManager,它生成一个我想在当前viewControllers视图上显示的UIView。但是当我尝试以下面这样的方式进行操作时,它会将视图置于viewController视图的顶部但不考虑导航栏。
如何在我的应用中的自定义视图上将视图放在navigationBar下面?
- (void)showNoConnectionMessage
{
UIViewController *currentController = [ExceptionManager getTopController];
UIView *currentView = currentController.view;
ExceptionMessageVC *exceptionMessageVC = [[ExceptionMessageVC alloc] initWithMessage:@"No connection!"];
exceptionMessageVC.view.translatesAutoresizingMaskIntoConstraints = NO;
[currentView addSubview:exceptionMessageVC.view];
[currentView addConstraint:[NSLayoutConstraint constraintWithItem:exceptionMessageVC.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:currentView attribute:NSLayoutAttributeHeight multiplier:0.1f constant:0]];
[currentView addConstraint:[NSLayoutConstraint constraintWithItem:exceptionMessageVC.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:currentView attribute:NSLayoutAttributeWidth multiplier:1.f constant:0]];
[currentView addConstraint:[NSLayoutConstraint constraintWithItem:exceptionMessageVC.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:currentView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0]];
[currentView addConstraint:[NSLayoutConstraint constraintWithItem:exceptionMessageVC.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:currentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0]];
}
+ (UIViewController*) getTopController
{
UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topViewController.presentedViewController) {
topViewController = topViewController.presentedViewController;
}
return topViewController;
}
答案 0 :(得分:0)
使用约束时,您可以使用topLayoutGuide
的{{1}}和bottomLayoutGuide
属性。他们考虑了导航栏和底栏。
而不是:
UIViewController
这样做:
[currentView addConstraint:[NSLayoutConstraint constraintWithItem:exceptionMessageVC.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:currentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0]];
答案 1 :(得分:0)
添加您的视图:
[[UIApplication sharedApplication].delegate.window addSubview:exceptionMessageVC.view];
答案 2 :(得分:0)
通过修改getTopController来解决它:
+ (UIViewController *)getTopController
{
UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([topViewController.childViewControllers.lastObject isKindOfClass:[UITabBarController class]])
{
topViewController = ((UITabBarController *) topViewController.childViewControllers.lastObject).selectedViewController;
}
if (topViewController.childViewControllers.lastObject)
topViewController = topViewController.childViewControllers.lastObject;
while (topViewController.presentedViewController)
{
topViewController = topViewController.presentedViewController;
}
return topViewController;
}