动态创建的字段和按钮在iOS中无法单击

时间:2017-03-08 20:56:32

标签: ios objective-c

我正在构建一个简单的登录屏幕。我创建了几个动态字段,将一个监听器附加到按钮上,但是我无法点击这些元素来开始输入我的用户名和密码。如果我按TAB键,焦点会进入输入但我无法使用点击。我做错了什么?

请参阅下面的代码和屏幕截图。以红色突出显示的区域无法点击,并且我已启用辅助功能检查器。它从未显示我的任何元素。

@interface PRLoginController() <UITextFieldDelegate>

@property (nonatomic) UIView *inputView;
@property (nonatomic) UITextField *usernameTextField;
@property (nonatomic) UITextField *passwordTextField;
@property (nonatomic) UIButton *loginButton;
@property (nonatomic) UIButton *cancelButton;
- (void)doLogin;
- (void)doCancel;

- (void)initializeUserInput;

+ (UIViewController *)getTopViewController;

@end


@implementation PRLoginController


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"Login";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:nil action:nil];

    [self initializeUserInput];
}


- (void)initializeUserInput {

    UIView *inputView = [[UIView alloc] init];
    inputView.translatesAutoresizingMaskIntoConstraints = NO;

    CGFloat inputViewMargin = 50.0;
    CGFloat inputElementTopMargin = 20.0;

    [self.view addSubview:inputView];
    [self.view addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0],
                                [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0],
                                [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:inputViewMargin],
                                [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-inputViewMargin]
                                ]];

    _inputView = inputView;

    CGFloat textFieldHeight = 30.0;

    UITextField *topTextField = [[UITextField alloc] init];
    topTextField.translatesAutoresizingMaskIntoConstraints = NO;

    [inputView addSubview:topTextField];
    [inputView addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:topTextField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0],
                                [NSLayoutConstraint constraintWithItem:topTextField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0],
                                [NSLayoutConstraint constraintWithItem:topTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0],
                                [NSLayoutConstraint constraintWithItem:topTextField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:textFieldHeight],
                                ]];

    UITextField *bottomTextField = [[UITextField alloc] init];
    bottomTextField.translatesAutoresizingMaskIntoConstraints = NO;

    [inputView addSubview:bottomTextField];
    [inputView addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:bottomTextField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:topTextField attribute:NSLayoutAttributeBottom multiplier:1.0 constant:inputElementTopMargin],
                                [NSLayoutConstraint constraintWithItem:bottomTextField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0],
                                [NSLayoutConstraint constraintWithItem:bottomTextField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0],
                                [NSLayoutConstraint constraintWithItem:bottomTextField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:textFieldHeight]
                                ]];

    topTextField.borderStyle = UITextBorderStyleRoundedRect;
    bottomTextField.borderStyle = UITextBorderStyleRoundedRect;

    topTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    bottomTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    topTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    bottomTextField.autocorrectionType = UITextAutocorrectionTypeNo;

    topTextField.returnKeyType = UIReturnKeyDone;
    bottomTextField.returnKeyType = UIReturnKeyDone;

    topTextField.delegate = self;
    bottomTextField.delegate = self;

    topTextField.placeholder = @"Username";
    bottomTextField.placeholder = @"Password";

    _usernameTextField = topTextField;
    _passwordTextField = bottomTextField;

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.translatesAutoresizingMaskIntoConstraints = NO;

    [inputView addSubview:leftButton];
    [inputView addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:leftButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:150],
                                [NSLayoutConstraint constraintWithItem:leftButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0],
                                [NSLayoutConstraint constraintWithItem:leftButton attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:-5.0],
                                ]];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.translatesAutoresizingMaskIntoConstraints = NO;

    [inputView addSubview:rightButton];
    [inputView addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:rightButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:150],
                                [NSLayoutConstraint constraintWithItem:rightButton attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0],
                                [NSLayoutConstraint constraintWithItem:rightButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:inputView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:5.0],
                                ]];

    leftButton.backgroundColor = [UIColor grayColor];
    rightButton.backgroundColor = leftButton.backgroundColor;

    leftButton.layer.cornerRadius = 4.0;
    rightButton.layer.cornerRadius = leftButton.layer.cornerRadius;


    [leftButton setTitle:@"Login" forState:UIControlStateNormal];
    [rightButton setTitle:@"Cancel" forState:UIControlStateNormal];


    [leftButton addTarget:self action:@selector(doLogin) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self action:@selector(doCancel) forControlEvents:UIControlEventTouchUpInside];

    _loginButton = leftButton;
    _cancelButton = rightButton;

}

enter image description here enter image description here enter image description here

修改

我删除了手势识别器,但没有帮助。单击ui不会执行任何操作。我对这个处理程序并不特别感兴趣,它是从不同的视图复制的,但删除它也无济于事。

1 个答案:

答案 0 :(得分:2)

inputView的大小可能不正确,导致所有触摸都超出其范围,因此无法传递给子视图。尝试添加这些约束

[NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute: NSLayoutAttributeTop multiplier:1.0 constant:-inputViewMargin],
[NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute: NSLayoutAttributeBottom multiplier:1.0 constant:-inputViewMargin]

inputView垂直填充它的容器。你似乎已经限制了前导和尾随(左边和右边),但也需要做顶部和底部。