2017-03-08 50 views
1

我正在構建簡單的登錄屏幕。我創建了一些動態字段,將一個監聽器附加到按鈕上,但是我無法點擊這些元素來開始輸入我的用戶名和密碼。如果按Tab鍵,焦點會進入輸入,但我無法使用該點擊。我究竟做錯了什麼?動態創建的字段和按鈕在iOS中不可點擊

請參閱下面的代碼和屏幕截圖。以紅色突出顯示的區域不可點擊,並且我已啓用輔助功能檢查器。它從不顯示我的任何元素。

@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

爲什麼在根視圖上有一個輕擊手勢識別器? – NRitH

+0

@NRitH輕觸識別器似乎是將文本框中的輕擊鍵盤關閉。 @ caiocpricci2你可以在識別器處理程序中添加一個日誌,我想它是攔截觸摸並且不允許通過文本字段。點擊文本字段,查看點擊識別器日誌是否被觸發。你也不需要添加'inputView'並設置它的約束兩次。 –

+0

嘗試移除輕擊識別器,然後查看您是否獲得了期望的文本字段行爲......如果是這樣,我們可以查看「UIGestureRecignizerDelegate」來正確解決它。 –

回答

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垂直填充它的容器。你似乎已經約束了前導和尾隨(左邊和右邊),但也需要頂端和底端。

相關問題