zoukankan      html  css  js  c++  java
  • iOS UIButton加在window上点击无效果问题

    UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible];

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
        ViewController *vc = [[ViewController alloc] init];
        self.window.rootViewController = vc;
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        
        view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 200, 200, 100)];
        view1.backgroundColor = [UIColor blueColor];
        [self.window addSubview:view1];
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(60, 200, 50, 150)];
        view2.backgroundColor = [UIColor yellowColor];
        [self.window addSubview:view2];
        
        
        UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(60, 400, 200, 100)];
        view3.backgroundColor = [UIColor redColor];
        [self.window addSubview:view3];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(60, 510, 100, 35);
        button.userInteractionEnabled = YES;
        button.backgroundColor = [UIColor greenColor];
        [button setTitle:@"change" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:button];
        NSLog(@"%@",self.window.subviews);
    - (void)changeView
    {
        [self.window bringSubviewToFront:view1];
         NSLog(@"%@",self.window.subviews);
    }

    不要直接加在window上,加在ViewController view上点击有效果

    view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 200, 200, 100)];
        view1.backgroundColor = [UIColor blueColor];
        [self.view addSubview:view1];
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(60, 200, 50, 150)];
        view2.backgroundColor = [UIColor yellowColor];
        view2.tag = 2;
        [self.view addSubview:view2];
        
        
        UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(60, 400, 200, 100)];
        view3.backgroundColor = [UIColor redColor];
        [self.view addSubview:view3];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(60, 510, 100, 35);
        button.userInteractionEnabled = YES;
        button.backgroundColor = [UIColor greenColor];
        [button setTitle:@"change" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        NSLog(@"%@",self.view.subviews);
    - (void)changeView
    {
        [self.view bringSubviewToFront:view1];
        UIView *view = [self.view viewWithTag:2];
        view.backgroundColor = [UIColor purpleColor];
        NSLog(@"%@",self.view.subviews);
    }

  • 相关阅读:
    【uniapp】改善中大型uniapp小程序项目开发体验
    vite试玩:老项目启动从6分钟到秒开
    修剪AST树减少webapck打包尺寸
    librispeech数据集下载
    语音识别性能评估方法
    2021.12.11 物联网考试
    2021.12.15 课程总结+加分项
    2021.12.9 观影大数据分析
    2021.12.8 Docker服务
    2021.12.10 阿里云服务器创建
  • 原文地址:https://www.cnblogs.com/ycclmy/p/5631496.html
Copyright © 2011-2022 走看看