gpt4 book ai didi

ios - 在输入附件 View 中显示带有 UITextField 的键盘

转载 作者:技术小花猫 更新时间:2023-10-29 10:09:05 25 4
gpt4 key购买 nike

我有一个 buttonA,当单击 buttonA 时,我希望键盘在 inputAccessoryView 中显示一个 UITextField。有没有一种方法可以手动显示键盘并设置 inputAccessoryView 而最初没有 UITextField?

谢谢。

最佳答案

如果没有可以成为第一响应者的对象,则无法召唤键盘。有两种解决方法:

  • 子类UIView 并在其中实现UIKeyInput 协议(protocol)。例如:

    在你的 .h 文件中:

    @interface InputObject : UIView<UIKeyInput>
    @property (nonatomic, copy) NSString *text;
    @property (nonatomic, strong) UIView *inputAccessoryView; // You must override inputAccessoryView , since it's readonly by default
    @end

    在您的 .m 文件中,实现协议(protocol):

    - (BOOL)canBecomeFirstResponder
    {
    return YES;
    }

    - (BOOL)hasText
    {
    return [self.text length];
    }

    - (void)insertText:(NSString *)text
    {
    NSString *selfText = self.text ? self.text : @"";
    self.text = [selfText stringByAppendingString:text];
    [[NSNotificationCenter defaultCenter] postNotificationName:kInputObjectTextDidChangeNotification object:self];
    }

    - (void)deleteBackward
    {
    if ([self.text length] > 0)
    self.text = [self.text substringToIndex:([self.text length] - 1)];
    else
    self.text = nil;
    [[NSNotificationCenter defaultCenter] postNotificationName:kInputObjectTextDidChangeNotification object:self];
    }

假设您想在 -viewDidAppear: 中调用键盘,代码如下:

    - (void)viewDidLoad
{
[super viewDidLoad];
// inputObject and textField are both your ivars in your view controller
inputObject = [[InputObject alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
inputObject.inputAccessoryView = textField;
[self.view addSubview:inputObject];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputObjectTextDidChange:) name:kInputObjectTextDidChangeNotification object:nil];

}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[inputObject becomeFirstResponder]; // This will summon the keyboard
}

然后在你的 View Controller 中实现通知选择器:

    - (void)inputObjectTextDidChange:(NSNotification *)notification
{
// Just set the text here. notification.object is actually your inputObject.
textField.text = ((InputObject *)(notification.object)).text;
}

这可能就是您所说的“最初没有 UITextField 的情况下设置 inputAccessoryView”

  • 另一种解决方法是通过仔细安排其动画,让文本字段“假装”为 inputAccessoryView。但此解决方案需要您的 textField 成为第一响应者。

首先,您在 -viewDidLoad 中观察键盘事件:

    - (void)viewDidLoad
{
[super viewDidLoad];
// Init the textField and add it as a subview of self.view
textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
[self.view addSubview:textField];

// Register keyboard events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}

其次,在 -viewWillAppear: 中设置您的 textField 的框架,以保证其框架不会受到自动调整的影响:

    - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
textField.frame = CGRectMake(0, CGRectGetMaxY(self.view.bounds), CGRectGetWidth(self.view.bounds), 50);
}

然后,安排您的textField 动画,让它与键盘动画同步。您的键盘通知选择器可能是这样的:

    - (void)keyboardWillShowNotification:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
UIViewAnimationCurve curve = [[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat duration = [[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect keyboardFrame = [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame toView:self.view];

[UIView animateWithDuration:duration delay:0.0f options:(UIViewAnimationOptions)curve animations:^{
CGRect textFieldFrame = textField.frame;
textFieldFrame.origin.y = keyboardFrame.origin.y - CGRectGetHeight(textFieldFrame);
textField.frame = textFieldFrame;
}completion:nil];
}

- (void)keyboardWillHideNotification:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
UIViewAnimationCurve curve = [[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat duration = [[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration delay:0.0f options:(UIViewAnimationOptions)curve animations:^{
textField.frame = CGRectMake(0, CGRectGetMaxY(self.view.bounds), CGRectGetWidth(self.view.bounds), 50);
}completion:nil];
}

最后调用[textField becomeFirstResponder]触发动画。

关于ios - 在输入附件 View 中显示带有 UITextField 的键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18261354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com