0

この の中にとがあるシンプルなものを作成しましたUIView。これは画面では問題なく表示されますが、 をタッチすると、キーボードが表示されません。UILabelUITextFieldUIViewUITextField

私はautolayoutプログラムを使用していますが、問題はUIVIewフレームに関するものだと思います。この問題をシミュレートする簡単なコードを作成しました。コードの最も関連性の高い部分は次のとおりです。

FormInput インターフェイス。

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

@interface FormInput : UIView

@property (nonatomic, strong) NSString *title;

@end

FormInput の実装

// 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 に答える