ios – 在输入附件视图中显示带有UITextField的键盘

前端之家收集整理的这篇文章主要介绍了ios – 在输入附件视图中显示带有UITextField的键盘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个buttonA,当buttonA被点击时,我希望键盘在inputAccessoryView中显示一个UITextField.有没有办法手动显示键盘,还要设置inputAccessoryView最初没有UITextField?

谢谢.

解决方法

您无法召唤键盘,而无需成为第一反应者的对象.有两种方法可以解决

>将UIView子类化并在其中实现UIKeyInput协议.例如:

在.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文件中,实现协议:

- (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,100,100)];
        textField = [[UITextField alloc] initWithFrame:CGRectMake(0,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
    }

然后在视图控制器中实现通知选择器:

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

这可能是你的意思是“设置inputAccessoryView最初没有UITextField”

>另一个解决方法是让textField“假装”通过仔细安排其动画来成为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,50);
        }completion:nil];
    }

最后,调用[textField成为FirstResponder]来触发动画.

原文链接:https://www.f2er.com/iOS/336533.html

猜你在找的iOS相关文章