pronebird
pronebird

Reputation: 12240

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

Upvotes: 1

Views: 656

Answers (1)

pronebird
pronebird

Reputation: 12240

Turned out the missing piece was:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

Very frustrating that UIInputViewController does not do this automatically.

Upvotes: 3

Related Questions