触摸UITextField时不显示键盘

时间:2013-10-18 14:45:39

标签: ios objective-c keyboard uitextfield autolayout

我在UIView内创建了一个简单UILabel UITextFieldUIView,这在屏幕上看起来不错,但当我触及{{1}时键盘没有出现。

我正在使用自动布局程序化,我认为这个问题与UITextField帧有关。 我做了一个简单的代码来模拟这个问题,代码中最相关的部分是:

FormInput接口。

UIVIew

FormInput实现

//FormInput.h
#import <UIKit/UIKit.h>

@interface FormInput : UIView

@property (nonatomic, strong) NSString *title;

@end

查看控制器

// FormInput.m
#import "FormInput.h"

@interface FormInput () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel       *label;
@property (nonatomic, strong) UITextField   *field;

- (void)setuSubviews;

@end

@implementation FormInput

- (id)init
{
    self = [super init];

    if(!self) return nil;

    [self setuSubviews];

    return self;
}

- (void)setuSubviews
{
    NSDictionary *views = @{
                            @"label":self.label,
                            @"field":self.field
                            };

    [self addSubview:self.label];
    [self addSubview:self.field];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[label(<=120)]-2-[field]-0-|" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[label]" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[field]" options:0 metrics:nil views:views]];
}


#pragma mark - Lazy properties

- (UILabel *)label
{
    if(!_label)
    {
        _label = [UILabel new];
        _label.textColor = [UIColor blackColor];
        _label.translatesAutoresizingMaskIntoConstraints = NO;
    }

    return _label;
}

- (UITextField *)field
{
    if(!_field)
    {
        _field = [UITextField new];
        _field.translatesAutoresizingMaskIntoConstraints = NO;
        _field.font = [UIFont systemFontOfSize:16];
        _field.placeholder = @"enter text here";
        _field.keyboardType = UIKeyboardTypeDefault;
        _field.keyboardAppearance = UIKeyboardAppearanceDark;
        _field.returnKeyType = UIReturnKeyDone;
        _field.clearButtonMode = UITextFieldViewModeWhileEditing;
        _field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }

    return _field;
}



#pragma mark - Properties setters 

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.label.text = title;
}

@end

2 个答案:

答案 0 :(得分:0)

确保相关文本字段的父容器具有“已启用用户交互”已选中。也许你检查了它的文本字段而不是你的viewcont?

我的声誉是“1”而且我不能发布图像,但是这个复选按钮应该在交互部分......

答案 1 :(得分:0)

好的,我找到了原因。

下面约束中每个视图的高度仍然是固有的:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[username]-30-[email]-30-[btn]-30-[field]" options:0 metrics:nil views:views]]

因此,只需在下面的约束视觉格式中为每个视图添加垂直高度。

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[username(20)]-30-[email(20)]-30-[btn]-30-[field]" options:0 metrics:nil views:views]]

不是微不足道的,但这会使添加视图中的视图响应触摸操作。

谢谢!