Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
在自己写的uiwindow里,需要添加下面一句
window_.rootViewController = [[UIViewController alloc] init];
全部代码如下
//
// TGTopWindow.h
// baisibudejie
//
// Created by targetcloud on 2017/5/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TGTopWindow : NSObject
+ (void)show;
+ (void)hide;
@end
//
// TGTopWindow.m
// baisibudejie
//
// Created by targetcloud on 2017/5/26.
// Copyright © 2017年 targetcloud. All rights reserved.
//
#import "TGTopWindow.h"
@implementation TGTopWindow
static UIWindow *window_;
+ (void)initialize{
window_ = [[UIWindow alloc] init];
window_.rootViewController = [[UIViewController alloc] init];
window_.frame = CGRectMake(0, 0, ScreenW, 20);
window_.backgroundColor = [UIColor clearColor];
window_.windowLevel = UIWindowLevelAlert;
[window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClick)]];
}
+ (void)show{
window_.hidden = NO;
}
+ (void)hide{
window_.hidden = YES;
}
+ (void)windowClick{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self searchScrollViewInView:window];
}
+ (void)searchScrollViewInView:(UIView *)superview{
for (UIScrollView *subview in superview.subviews) {
if ([subview isKindOfClass:[UIScrollView class]] && subview.isShowingOnKeyWindow) {
CGPoint offset = subview.contentOffset;
offset.y = - subview.contentInset.top;
[subview setContentOffset:offset animated:YES];
}
[self searchScrollViewInView:subview];
}
}
@end
//
// UIView+frame.h
// baisibudejie
//
// Created by targetcloud on 2017/3/6.
// Copyright © 2017年 targetcloud. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (frame)
@property CGFloat width;
@property CGFloat height;
@property CGFloat x;
@property CGFloat y;
@property CGFloat centerX;
@property CGFloat centerY;
@property CGSize size;
+ (instancetype)viewFromXIB;
- (BOOL)isShowingOnKeyWindow;
@end
//
// UIView+frame.m
// baisibudejie
//
// Created by targetcloud on 2017/3/6.
// Copyright © 2017年 targetcloud. All rights reserved.
//
#import "UIView+frame.h"
@implementation UIView (frame)
+ (instancetype)viewFromXIB{
return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}
- (BOOL)isShowingOnKeyWindow{
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
CGRect winBounds = keyWindow.bounds;
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}
-(CGSize)size{
return self.frame.size;
}
-(void)setSize:(CGSize)size{
CGRect rect = self.frame;
rect.size = size;
self.frame = rect;
}
- (void)setHeight:(CGFloat)height{
CGRect rect = self.frame;
rect.size.height = height;
self.frame = rect;
}
- (CGFloat)height{
return self.frame.size.height;
}
- (CGFloat)width{
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width{
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
}
- (CGFloat)x{
return self.frame.origin.x;
}
- (void)setX:(CGFloat)x{
CGRect rect = self.frame;
rect.origin.x = x;
self.frame = rect;
}
- (void)setY:(CGFloat)y{
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
- (CGFloat)y{
return self.frame.origin.y;
}
- (void)setCenterX:(CGFloat)centerX{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
- (CGFloat)centerX{
return self.center.x;
}
- (void)setCenterY:(CGFloat)centerY{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
- (CGFloat)centerY{
return self.center.y;
}
@end
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'