UITextField作为子图层

时间:2012-05-31 10:13:51

标签: iphone ipad calayer

我将UITextField的图层添加为现有图层(实际上是UIImageView)的子图层。我在图像视图上应用透视旋转,它工作正常。但是UITextField不会变为活动状态。它的beginFirstResponder消息不显示键盘。点击它不起作用,它在用户交互中保持不活动状态。我还尝试为文本字段的图层设置zPosition,使其位于imageview图层和zPosition之上。

    rightband = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right-band.png"]];
    rightband.layer.frame = CGRectMake(623.5, 310.5, 624, 210);
    rightband.layer.zPosition = 40.0f;


    usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 260, 26)];
    usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    usernameField.keyboardType = UIKeyboardTypeDefault;
    usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    usernameField.borderStyle = UITextBorderStyleNone;
    usernameField.backgroundColor = [UIColor clearColor];
    usernameField.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    usernameField.textColor = UIColorFromRGB(0x303030);
    usernameField.textAlignment = UITextAlignmentLeft;
    usernameField.placeholder = @"Username";
    usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
    usernameField.returnKeyType = UIReturnKeyNext;
    usernameField.delegate = self;
    usernameField.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
    usernameField.layer.zPosition = 100.0f;
    usernameField.layer.frame = CGRectMake(10, 10, 260, 26); 
    [rightband.layer addSublayer:usernameField.layer];

2 个答案:

答案 0 :(得分:0)

您可以尝试将视图heirarchy更改为以下

UIView - > UIImageView和UITextField 不要将UITextField作为子项添加到UIImageView UITextField和UIImageView将是UIView的孩子

答案 1 :(得分:0)

您应该添加文本字段视图,以便它响应触摸。 所以:

[rightband addSubview:usernameField];