0

我创建了一个简单UIView的 aUILabel和 a ,UITextFieldUIView在屏幕上看起来很好,但是当我触摸UITextField,键盘没有出现。

我正在使用自动布局编程,我认为问题出在UIVIew框架上。我做了一个简单的代码来模拟这个问题,代码中最相关的部分是:

表单输入接口。

//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

视图控制器

#import "ViewController.h"
#import "FormInput.h"

@interface ViewController ()

@property (nonatomic, strong) FormInput *username;
@property (nonatomic, strong) FormInput *email;

- (void)setupLayout;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self setupLayout];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self setupLayout];
}


#pragma mark - Lazy properties

- (FormInput *)username
{
    if(!_username)
    {
        _username = [FormInput new];
        _username.translatesAutoresizingMaskIntoConstraints = NO;
        _username.title = @"Username:";
    }

    return _username;
}

- (FormInput *)email
{
    if(!_email)
    {
        _email = [FormInput new];
        _email.translatesAutoresizingMaskIntoConstraints = NO;
        _email.title = @"Email:";
    }

    return _email;
}


#pragma mark - Layout setup

- (void)setupLayout
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.translatesAutoresizingMaskIntoConstraints = NO;
    [btn setTitle:@"Button" forState:UIControlStateNormal];

    UITextField *field = [UITextField new];
    field.translatesAutoresizingMaskIntoConstraints = NO;
    field.placeholder = @"Placeholder";

    [self.view addSubview:self.username];
    [self.view addSubview:self.email];
    [self.view addSubview:btn];
    [self.view addSubview:field];

    NSDictionary *views = @{
                            @"username" : self.username,
                            @"email"    : self.email,
                            @"btn"      : btn,
                            @"field"    : field
                            };

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

@end
4

2 回答 2

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]]

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

谢谢!

于 2013-10-18T18:51:12.273 回答
0

确保相关文本字段的父容器已选中“启用用户交互” 。也许您检查了它的文本字段但没有检查您的视图?

我的声誉是“1”,我不能发布图片,但是这个检查按钮应该在交互部分......

于 2013-10-18T14:52:53.897 回答