【问题标题】:iOS Custom keyboard increase UIInputViewController heightiOS 自定义键盘增加 UIInputViewController 高度
【发布时间】:2019-08-23 11:15:29
【问题描述】:

我只是无法理解文档所说的如何让我的 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

感谢这里的任何提示。

【问题讨论】:

    标签: ios objective-c custom-keyboard


    【解决方案1】:

    基于开发者代码级别票证:

    1. 将按钮添加到 UIView(比如 keysView
    2. keysView 添加到 UIInputViewController 的视图中。
    3. 添加问题中的约束
    4. 使keysView.frameself.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
    

    【讨论】:

      猜你喜欢
      • 2017-01-30
      • 2017-04-20
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 2018-05-24
      • 2015-04-18
      • 2018-07-25
      相关资源
      最近更新 更多