简体   繁体   English

iOS自定义键盘增加UIInputViewController高度

[英]iOS Custom keyboard increase UIInputViewController height

I just cannot get what the docs say about getting the height of my iOS custom keyboard working. 我只是无法得到文档所说的关于让我的iOS自定义键盘工作的高度。

This is clean Keyboard target and added what seems to be apple docs and many SO answers as the right answer, but it does not work on XS and 6S simulators: 这是干净的键盘目标,并添加了似乎是苹果文档和许多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

在此输入图像描述

Grateful for any tips here. 感谢这里的任何提示。

Based on Developer Code Level ticket: 基于开发人员代码级别票证:

  1. add your buttons to a UIView (say keysView ) 将您的按钮添加到UIView(比如keysView
  2. add the keysView to the UIInputViewController's view. keysView添加到UIInputViewController的视图中。
  3. Add the constraint as in the question 像问题一样添加约束
  4. Make keysView.frame the same as self.view keysView.frame一样self.view
  5. Add at least one constant to one of the buttons to keysView 将至少一个常量添加到keysView其中一个按钮

Now you should have an expanding keysView which is as high as the self.view . 现在,你应该有一个不断扩大keysView是一样高的self.view

Full code: 完整代码:



#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM