2015-11-26 138 views
0

是否可以在iOS 9上使用UIInputViewController子類作爲輸入視圖?我已經嘗試設置inputViewController屬性,但沒有自定義視圖控制器顯示時,我的文本字段成爲第一響應者。自定義輸入視圖控制器

@interface InputViewController : UIInputViewController 

@end 

@implementation InputViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 
    datePicker.translatesAutoresizingMaskIntoConstraints = NO; 

    [self.inputView addSubview:datePicker]; 

    [datePicker.leadingAnchor constraintEqualToAnchor:self.inputView.leadingAnchor].active = YES; 
    [datePicker.trailingAnchor constraintEqualToAnchor:self.inputView.trailingAnchor].active = YES; 
    [datePicker.topAnchor constraintEqualToAnchor:self.inputView.topAnchor].active = YES; 
    [datePicker.bottomAnchor constraintEqualToAnchor:self.inputView.bottomAnchor].active = YES; 
} 

@end 

@interface TextField: UITextField 

@property (nonatomic, readwrite, strong) UIInputViewController *inputViewController; 

@end 

@implementation TextField 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.textField.inputViewController = [[InputViewController alloc] init]; 
} 

@end 

回答

1

原來缺少的部分是:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

非常沮喪的是UIInputViewController不會自動執行此操作。

相關問題