How can I add UIView object to self.view.window in ios7?

Solution 1

Here's one way of doing what you want:

[[UIApplication sharedApplication].keyWindow addSubview:overlayView];

Solution 2

According to the documentation of UIView, the window property is nil if the view has not yet been added to a window which is the case when viewDidLoad is called.

You can do the same thing in viewDidAppear:(BOOL)animated;

- (void)viewDidAppear:(BOOL)animated; {
    UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    overlayView.backgroundColor = [UIColor blackColor];
    overlayView.alpha = 0.8;
    [self.view.window addSubview:overlayView];
}

Solution 3

Swift 3

UIApplication.shared.keyWindow?.addSubview(overlayView);
Share:
Admin
Author by

Admin

Updated on June 26, 2022

Comments

  • Admin
    Admin about 1 month

Related