Orientation in a UIView added to a UIWindow

后端 未结 6 1865
眼角桃花
眼角桃花 2020-11-28 01:21

I have a UIView which is supposed to cover the whole device (UIWindow) to support an image zoom in/out effect I\'m doing using core animation where a user taps a button on a

6条回答
  •  無奈伤痛
    2020-11-28 02:18

    I created a category on UIApplication that has a helper property and method for getting the first subview of the keyWindow. This is the view you want to overlay anyway. Now when you add a view that is managed by a UIViewController to that view, the shouldRotateToInterfaceOrientation: method is called.

    UIApplication+WindowOverlay.h

    #import 
    
    @interface UIApplication(WindowOverlay)
    
        @property (nonatomic, readonly) UIView *baseWindowView;
    
        -(void)addWindowOverlay:(UIView *)view;
    
    @end
    

    UIApplication+WindowOverlay.m

    #import "UIApplication+WindowOverlay.h"
    
    @implementation UIApplication(WindowOverlay)
    
        -(UIView *)baseWindowView{
            if (self.keyWindow.subviews.count > 0){
                return [self.keyWindow.subviews objectAtIndex:0];
            }
            return nil;
        }
    
        -(void)addWindowOverlay:(UIView *)view{
            [self.baseWindowView addSubview:view];
        }
    @end
    

    and here is how you would use it.

    //at the top of the file...or in {yourproject}.pch
    #import "UIApplication+WindowOverlay.h
    
    //in a method:
    
    UIView *view = [UIView new];
    
    UIView *window = [UIApplication sharedApplication].baseWindowView;
    
    view.frame = window.bounds;
    
    [window addSubview:view];
    
    //or
    
    [[UIApplication sharedApplication] addWindowOverlay:view];
    

提交回复
热议问题