美文网首页
iOS Window

iOS Window

作者: jemmy_xl | 来源:发表于2021-07-17 11:57 被阅读0次

    问题:
    使用了YYText 后会存在输入框显示不出 copy,paste等 menu。

    代码接口逻辑:
    在VC添加一个 自定义alertView。alertView是添加在自定义window上面点。

    核心代码如下

    #import "AlertViewController.h"
    #import <YYText.h>
    
    @interface AlertViewController ()
    
    @property (nonatomic, strong) UIWindow *window;
    @property (nonatomic, copy) void (^showCompletionBlock)(void);
    @property (nonatomic, strong) YYTextView *View11;
    
    
    
    @end
    
    @implementation AlertViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
     
        self.View11 = [[YYTextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        [self.view addSubview:self.View11];
        self.View11.backgroundColor = UIColor.redColor;
        
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 210, 50, 50)];
        btn.backgroundColor = [UIColor grayColor];
        [btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
        
        UITextView *bt22n = [[UITextView alloc] initWithFrame:CGRectMake(300, 210, 100, 0100)];
        bt22n.backgroundColor = [UIColor blueColor];
         [self.view addSubview:bt22n];
        
    }
    
    - (void)clickBtn
    {
        [self dismissInWindowAnimateCompletion:nil];
    }
    
    - (UIWindow *)window
    {
        if (!_window) {
            _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
            _window.windowLevel = UIWindowLevelAlert;
            _window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            _window.opaque = NO;
        }
        return _window;
    }
    
    - (void)showAnimated:(NSInteger)level completion:(void (^)(void))completion
    {
         self.showCompletionBlock = completion;
        
        self.window.windowLevel = level;
        
        if (level == 1000) {
            self.window.backgroundColor = UIColor.yellowColor;
            self.window.frame = CGRectMake(100, 100, 100, 100);
        }
        
        [self.window setRootViewController:self];
        [self.window makeKeyAndVisible];
        // 1.也可以直接使用window.hidden 这样就可以不使用 makeKeyAndVisible。
        // 2, 如果视图添加了 UITextField 或者 UITextView,当成为第一响应者的时候,系统会将 UITextField 或者 UITextView 所在的window 设置为keyWindow。
        // 3.添加了YYText(其他的第三方也存在可能性)后,如果存在 YYTextEffectWindow 那么该第二点将被打破。不会将 UITextField 或者 UITextView 所在的window 设置为keyWindow。
    //    self.window.hidden = NO;
    }
    
    - (void)dismissInWindowAnimateCompletion:(void (^)(void))completion
    {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeWindow];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.view.alpha = 1.0;
                if (completion) {
                    completion();
                }
            });
        }];
    }
    
    - (void)removeWindow
    {
        self.window.rootViewController = nil;
        self.window = nil;
        
    //    if (![[UIApplication sharedApplication].windows.firstObject isKindOfClass:[self class]]) {
    //        [[UIApplication sharedApplication].windows.firstObject makeKeyAndVisible];
    //    }
    }
    

    添加了YYText结果如下:


    image.png

    可以看出keyWindow 会变成 YYTextEffectWindow,

    不添加 YYText 结果如下:

    - (void)viewDidLoad {
        [super viewDidLoad];
     
    //    self.View11 = [[YYTextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    //    [self.view addSubview:self.View11];
    //    self.View11.backgroundColor = UIColor.redColor;
        
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 210, 50, 50)];
        btn.backgroundColor = [UIColor grayColor];
        [btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
        
        UITextView *bt22n = [[UITextView alloc] initWithFrame:CGRectMake(300, 210, 100, 0100)];
        bt22n.backgroundColor = [UIColor blueColor];
         [self.view addSubview:bt22n];
        
    }
    
    
    image.png

    解决思路:

    1. self.window = nil; 之后使用
      // if (![[UIApplication sharedApplication].windows.firstObject isKindOfClass:[self class]]) {
      // [[UIApplication sharedApplication].windows.firstObject makeKeyAndVisible];
      // }
      将keyWindo修改

    2. 修改库


      image.png

    在becomeKeyWindow 内加上 异步。

    相关文章

      网友评论

          本文标题:iOS Window

          本文链接:https://www.haomeiwen.com/subject/gmcwpltx.html