Пользовательская клавиатура iOS увеличивает высоту UIInputViewController - PullRequest
0 голосов
/ 02 апреля 2019

Я просто не могу понять, что в документах говорится о том, как заставить работать мою пользовательскую клавиатуру iOS.

Это чистая цель клавиатуры и добавление того, что кажется яблочными документами, и многих ответов SO в качестве правильного ответа,но он не работает на симуляторах XS и 6S:

//
//  KeyboardViewController.m
//  keyboard
//
//  Created by hiwa on 02/04/2019.
//  Copyright © 2019 hiwa. All rights reserved.
//

#import "KeyboardViewController.h"

@interface KeyboardViewController ()
@property (nonatomic, strong) UIButton *nextKeyboardButton;
@end

@implementation KeyboardViewController

- (void)updateViewConstraints {
    [super updateViewConstraints];

    // Add custom view sizing constraints here
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLayoutConstraint *heightConstraint =
    [NSLayoutConstraint constraintWithItem: self.view
                                 attribute: NSLayoutAttributeHeight
                                 relatedBy: NSLayoutRelationEqual
                                    toItem: nil
                                 attribute: NSLayoutAttributeNotAnAttribute
                                multiplier: 0.0
                                  constant: 300];
    [self.view addConstraint: heightConstraint];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // Perform custom UI setup here
    self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
    [self.nextKeyboardButton sizeToFit];

    [self.nextKeyboardButton addTarget:self action:@selector(handleInputModeListFromView:withEvent:) forControlEvents:UIControlEventAllTouchEvents];

    [self.view addSubview:self.nextKeyboardButton];

}

- (void)textWillChange:(id<UITextInput>)textInput {
    // The app is about to change the document's contents. Perform any preparation here.
}

- (void)textDidChange:(id<UITextInput>)textInput {
    // The app has just changed the document's contents, the document context has been updated.

    UIColor *textColor = nil;
    if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
        textColor = [UIColor whiteColor];
    } else {
        textColor = [UIColor blackColor];
    }
    [self.nextKeyboardButton setTitleColor:textColor forState:UIControlStateNormal];
}

@end

enter image description here

Благодарен за любые советы здесь.

1 Ответ

0 голосов
/ 07 апреля 2019

На основе тикета уровня кода разработчика:

  1. добавьте свои кнопки в UIView (скажем, keysView)
  2. добавьте keysView в представление UIInputViewController.
  3. Добавьте ограничение, как в вопросе
  4. Сделайте keysView.frame таким же, как self.view
  5. Добавьте хотя бы одну константу к одной из кнопок в keysView

Теперь у вас должен быть расширяющийся keysView, равный self.view.

Полный код:



#import "KeyboardViewController.h"

@interface KeyboardViewController ()

@property (nonatomic, strong) UIButton *nextKeyboardButton;

@end

@implementation KeyboardViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // (1)
    UIView *keysView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // (3)
    NSLayoutConstraint *keyboardHeightConstraint = [NSLayoutConstraint
                                                    constraintWithItem:self.view
                                                    attribute:NSLayoutAttributeHeight
                                                    relatedBy:NSLayoutRelationEqual
                                                    toItem:nil
                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                    multiplier:0.0
                                                    constant:310];
    [keyboardHeightConstraint setPriority:UILayoutPriorityDefaultHigh];
    [self.view addConstraints:@[keyboardHeightConstraint]];

    self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
    [self.nextKeyboardButton sizeToFit];
    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.nextKeyboardButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];

    [keysView addSubview:self.nextKeyboardButton];
    // (5)
    NSLayoutConstraint *nextKeyboardButtonLeftSideConstraint = [NSLayoutConstraint constraintWithItem:self.nextKeyboardButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:keysView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
    NSLayoutConstraint *nextKeyboardButtonBottomConstraint = [NSLayoutConstraint constraintWithItem:self.nextKeyboardButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:keysView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
    [keysView addConstraints:@[nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint]];
    // (4)
    keysView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    // (2)
    [self.view addSubview:keysView];
}

- (void)dealloc {
    self.nextKeyboardButton = nil;
}

@end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...