objective-cios9uiinputviewcontroller

Custom input view controller


Is it possible to use UIInputViewController subclass as input view on iOS 9? I've tried setting inputViewController property but no custom view controller shows up when my text field becomes a first responder.

@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

Solution

  • Turned out the missing piece was:

    self.view.translatesAutoresizingMaskIntoConstraints = NO;

    Very frustrating that UIInputViewController does not do this automatically.