关于基础视图(以UITextField为例)不响应事件和视图添加后却不出现的几种情况

本文探讨了UITextField不响应点击事件的常见原因,包括视图层级问题和未正确添加到父视图。同时,也提到了视图添加后无法显示的可能情况,如父视图为nil。

1.点击textField没有响应

(1)textField上面还有视图

<pre name="code" class="objc">   UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 220, 40)];
    tf.backgroundColor = [UIColor yellowColor];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [self.window addSubview:tf];
    [tf release];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    view.backgroundColor = [UIColor redColor];
    [self.window addSubview:view];
    [view release];



(2)textField 超出父视图的管理范围

视图能够管理它范围内所有的子视图,如果子视图超出了父视图的范围,就无法响应交互.

<pre name="code" class="objc">    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    bgView.backgroundColor = [UIColor redColor];
    //将自己视图内超出自身管理范围的子视图裁减掉.
    bgView.clipsToBounds = YES;
    [self.window addSubview:bgView];
    [bgView release];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, 320, 40)];
    tf.backgroundColor = [UIColor greenColor];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [bgView addSubview:tf];
    [tf release];



(3)自身问题引起的点击不响应.
 <pre name="code" class="objc">   UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 220, 40)];
    tf.backgroundColor = [UIColor greenColor];
    //设置能否编辑
//    tf.enabled = NO;
    //设置是否响应用户交互
    tf.userInteractionEnabled = NO;
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [self.window addSubview:tf];

[tf release];

(4)textField父视图不可交互UILabel默认的用户交互是关闭的
<pre name="code" class="objc">  UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    view.backgroundColor = [UIColor redColor];
    view.userInteractionEnabled = YES;
    [self.window addSubview:view];
    [view release];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 40, 320, 40)];
    tf.backgroundColor = [UIColor greenColor];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [view addSubview:tf];
    [tf release];



2.在屏幕上未看到创建的视图

(1)未添加到父视图

   UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    view.backgroundColor = [UIColor redColor];
    [self.window addSubview:view];
    [view release];
  (2)视图没有设置颜色,颜色依然为clearColor.
  (3)未设置frame
  (4)创建视图方法未调用

(5)添加的父视图为nil

   UIView *view = nil;
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    view1.backgroundColor = [UIColor redColor];
    [view addSubview:view1];
    [view1 release];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值