切换UITextField时调整键盘视图? [英] Adjust View For Keyboard When Switching UITextField?

查看:92
本文介绍了切换UITextField时调整键盘视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当键盘出现在我的应用程序中时,我能够将视图向上滑动,以便可以看到文本字段并且它工作得很好。但是,因为它基于键盘通知,它只在键盘出现时有效。

I have the ability for my view to slide up when the keyboard appears in my app so the text field can be seen and it worked just fine. However, because its based on keyboard notifications it only works when the keyboard appears.

含义,我选择一个文本字段,键盘出现,视图相应地向上滑动,但是如果然后我直接点击另一个文本字段视图无法调整,因为键盘已经存在。

Meaning, I select a textfield, the keyboard appears and the view slides up accordingly, but if I then tap directly onto another textfield the view doesn't adjust because the keyboard is already present.

这是我正在使用的代码,任何帮助适应这个工作如上所述的情况将非常感激。

Here is the code I am using, any help adapting this to work in a situation like above would be greatly appreciated.

-(void)registerForKeyboardNotifications
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    // add a tap gesture to drop first responder
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToHideKeyboard:)];
    [self.view addGestureRecognizer:tapGR];
}

-(void)keyboardDidShow:(NSNotification *)notification
{
    CGRect keyboardFrameW = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    CGRect keyboardFrame = [window convertRect:keyboardFrameW toView:self.view];

    //Have a minimum space between the keyboard and textfield
    CGFloat textFieldBuffer = 40;
    CGFloat textFieldKeyboardDifference = 0;

    if (activeTextField.frame.origin.y + activeTextField.frame.size.height > keyboardFrame.origin.y) textFieldKeyboardDifference = (activeTextField.frame.origin.y + activeTextField.frame.size.height + textFieldBuffer) - keyboardFrame.origin.y;
    else if (activeTextField.frame.origin.y + activeTextField.frame.size.height < keyboardFrame.origin.y) textFieldKeyboardDifference = 0;

    [self translateView:self.view toRect:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - textFieldKeyboardDifference, self.view.frame.size.width, self.view.frame.size.height) withDuration:0.3];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    //Revert to y origin 0
    [self translateView:self.view toRect:CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height) withDuration:0.3];
}

编辑:

当我调用 textFieldDidBeginEditing 时,我尝试手动调用键盘通知:

I have tried calling the keyboard notification manually when textFieldDidBeginEditing is called like this:

[self keyboardDidShow:[NSNotification notificationWithName:UIKeyboardDidShowNotification object:nil]]; 没有运气。该方法被调用,但没有进行调整,原因我无法解决。

[self keyboardDidShow:[NSNotification notificationWithName:UIKeyboardDidShowNotification object:nil]]; without luck. The method gets called, but no adjustment is made for a reason I can't work out.

推荐答案

你可能想要什么这里是提供所有 UITextField 的委托,并实现 - (void)textFieldDidBeginEditing:(UITextField *)textField 在委托上触发滚动操作。当有人开始在文本字段上编辑时,这将被调用,文本字段作为参数传递给方法。

What you probably want to do here is provide a delegate to all your UITextFields and implement - (void)textFieldDidBeginEditing:(UITextField *)textField on the delegate to trigger your scrolling action. This will be called any time someone starts editing on a text field, and the text field is passed in as a parameter to the method.

编辑:使用您的代码作为起点,这就是我想出的。每次更改文本字段时都会调用它:

Using your code as a starting point, this is what I came up with. It gets called on every change of text fields:

@interface SOViewController () <UITextFieldDelegate>
@property (nonatomic, readwrite, assign) UITextField* activeTextField;
@property (nonatomic, readwrite, assign) CGRect keyboardFrame;
@end

@implementation SOViewController

@synthesize activeTextField;
@synthesize keyboardFrame;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self registerForKeyboardNotifications];
    self.keyboardFrame = CGRectNull;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeTextField = textField;
    [self updatePosition];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeTextField = nil;
}

-(void)registerForKeyboardNotifications
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    //    // add a tap gesture to drop first responder
    //    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToHideKeyboard:)];
    //    [self.view addGestureRecognizer:tapGR];
}

- (void)translateView: (UIView*)view toRect: (CGRect)rect withDuration: (NSTimeInterval)duration
{
    NSLog(@"Translating view to rect: %@ overDuration: %g", NSStringFromCGRect(rect), duration);
}

- (void)updatePosition
{
    if (self.activeTextField && !CGRectIsNull(self.keyboardFrame))
    {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        CGRect localKeyboardFrame = [window convertRect: self.keyboardFrame toView:self.view];

        //Have a minimum space between the keyboard and textfield
        CGFloat textFieldBuffer = 40;

        CGRect textFieldFrame = self.activeTextField.frame;

        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y = 0;

        if (CGRectGetMaxY(textFieldFrame) + textFieldBuffer > CGRectGetMinY(localKeyboardFrame))
        {
            viewFrame.origin.y = -1.0 * (CGRectGetMaxY(textFieldFrame) + textFieldBuffer - CGRectGetMinY(localKeyboardFrame));
        }

        [self translateView: self.view toRect: viewFrame withDuration: 0.3];

    }
    else
    {
        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y = 0;
        [self translateView: self.view toRect: viewFrame withDuration: 0.3];
    }
}

-(void)keyboardDidShow:(NSNotification *)notification
{
    self.keyboardFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [self updatePosition];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    self.keyboardFrame = CGRectNull;
    [self updatePosition];
}

@end

这篇关于切换UITextField时调整键盘视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆