代码创建UIWindow -> UIView -> RootViewController中遇到UIview覆盖,控件事件不响应问题

1、问题描述:从

AppDelegate.m创建app窗口,定义rootviewcontroller时,在rootviewcontroller创建按钮,不响应事件动作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //UIWindow -> UIView -> RootViewController
    
    //****UIWindow
    //从代码里面创建第一个窗口window
    self.window = [[UIWindow alloc]init];
    //设置窗口的fram color
    self.window.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
    //还可以选择行改变顶上面导航栏的样式
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.backgroundColor = [UIColor clearColor];
    self.window.backgroundColor = [UIColor whiteColor];
    //设置rootviewcontroller
    ViewController *firstviewcontrol = [[ViewController alloc]init];
    [self.window setRootViewController:firstviewcontrol];
    
    UIView *rview = [[UIView alloc]initWithFrame:self.window.frame];
    [self.window addSubview:rview];
    rview.backgroundColor=[UIColor redColor];
    [self.window addSubview:rview];
    [self.window makeKeyAndVisible];
}

Viewcontroller.m中创建一个红色的Button.运行后点击button不会执行button tap事件。

UIButton *testbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    testbutton.frame = CGRectMake(50, 300, 100, 100);
    [testbutton setTitle:@"创建一个按钮" forState:UIControlStateNormal];
    //testbutton.backgroundColor = [UIColor clearColor];
    testbutton.backgroundColor = [UIColor redColor];
    [testbutton addTarget:self action:@selector(buttontap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testbutton];


找前辈帮忙分析,发现原因:在 AppDelegate.m中定义rootviewcontroller之后加载一个UIView会把,在rootviewcontroller中定义的view覆盖掉!

视图叠加的顺序和代码执行的顺序一致:UIwindow -> rootviewcontroller +(rootviewcontroller中的subview) -> rview


一般最好不要在AppDelegate.m中再加在其他UIview,最好都在各自的viewcontroller中创建UIView.如果非要创建注意叠加的顺序,或者在进入主控制器时remove这个view

修改成这个样子:

self.window = [[UIWindow alloc]init];
    //设置窗口的fram color
    self.window.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
    //还可以选择行改变顶上面导航栏的样式
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.backgroundColor = [UIColor clearColor];
    self.window.backgroundColor = [UIColor whiteColor];
    
    <span style="color:#ff0000;"> UIView *rview = [[UIView alloc]initWithFrame:self.window.frame];
    [self.window addSubview:rview];
    //rview.backgroundColor=[UIColor redColor];
    [self.window addSubview:rview];
    //设置rootviewcontroller</span>
    ViewController *firstviewcontrol = [[ViewController alloc]init];
    [self.window setRootViewController:firstviewcontrol];
    
    [self.window makeKeyAndVisible];


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值