Iphone 切换UITextField时是否调整键盘视图?

Iphone 切换UITextField时是否调整键盘视图?,iphone,objective-c,ipad,uitextfield,uikeyboard,Iphone,Objective C,Ipad,Uitextfield,Uikeyboard,当键盘出现在我的应用程序中时,我的视图可以向上滑动,这样就可以看到文本字段,并且工作正常。但是,由于其基于键盘通知,因此仅当键盘出现时才起作用 也就是说,我选择一个文本字段,键盘就会出现,视图也会相应地向上滑动,但是如果我直接点击另一个文本字段,视图就不会调整,因为键盘已经存在了 这是我正在使用的代码,如果您能帮助我适应上述情况,我们将不胜感激 -(void)registerForKeyboardNotifications { NSNotificationCenter *center =

当键盘出现在我的应用程序中时,我的视图可以向上滑动,这样就可以看到文本字段,并且工作正常。但是,由于其基于键盘通知,因此仅当键盘出现时才起作用

也就是说,我选择一个文本字段,键盘就会出现,视图也会相应地向上滑动,但是如果我直接点击另一个文本字段,视图就不会调整,因为键盘已经存在了

这是我正在使用的代码,如果您能帮助我适应上述情况,我们将不胜感激

-(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];
}
—(无效)RegisterWorkeryBoardNotifications
{
NSNotificationCenter*中心=[NSNotificationCenter defaultCenter];
[center addObserver:自选择器:@selector(keyboardDidShow:)名称:UIKeyboardDidShowNotification对象:nil];
[center addObserver:自选择器:@selector(keyboardWillHide:)名称:UIKeyboardWillHideNotification对象:nil];
//添加轻触手势以放下第一响应者
UITapGestureRecognizer*tapGR=[[UITapGestureRecognizer alloc]initWithTarget:自操作:@selector(tapToHideKeyboard:)];
[self.view addgesturecognizer:tapGR];
}
-(无效)键盘显示:(NSNotification*)通知
{
CGRect keyboardFrameW=[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
UIWindow*window=[[UIApplication sharedApplication]keyWindow];
CGRect keyboardFrame=[window convertRect:keyboardFrameW to视图:self.view];
//在键盘和文本字段之间留有最小的空间
CGFloat TEXTFIELDBUSTER=40;
CGFloat TEXTFIELD键盘差=0;
如果(activeTextField.frame.origin.y+activeTextField.frame.size.height>keyboardFrame.origin.y)textFieldKeyboardDifference=(activeTextField.frame.origin.y+activeTextField.frame.size.height+textFieldBuffer)-keyboardFrame.origin.y;
如果(activeTextField.frame.origin.y+activeTextField.frame.size.height
编辑:

我曾尝试在调用
textfielddibeginediting
时手动调用键盘通知,如下所示:


[self keyboardDidShow:[NSNotification notificationWithName:UIKeyboardDidShowNotification对象:nil]]您可能希望在此处为所有
UITextField
提供一个委托,并在委托上实现
-(void)textfieldDiBeginediting:(UITextField*)textField
,以触发滚动操作。每当有人开始编辑文本字段,并且文本字段作为参数传递给该方法时,就会调用该函数

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

@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
@interface-SOViewController()
@属性(非原子、读写、赋值)UITextField*activeTextField;
@属性(非原子、读写、赋值)CGRect键盘框;
@结束
@实现SOViewController
@合成活性文本场;
@综合键盘框架;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
[自注册BoardNotifications];
self.keyboardFrame=CGRectNull;
}
-(无效)textFieldDidBeginEditing:(UITextField*)textField
{
self.activeTextField=textField;
[自我更新位置];
}
-(void)textfielddidediting:(UITextField*)textField
{
self.activeTextField=nil;
}
-(无效)注册工董事会通知
{
NSNotificationCenter*中心=[NSNotificationCenter defaultCenter];
[center addObserver:自选择器:@selector(keyboardDidShow:)名称:UIKeyboardDidShowNotification对象:nil];
[center addObserver:自选择器:@selector(keyboardWillHide:)名称:UIKeyboardWillHideNotification对象:nil];
////添加轻触手势以放下第一响应者
//UITapGestureRecognizer*tapGR=[[UITapGestureRecognizer alloc]initWithTarget:自操作:@selector(tapToHideKeyboard:)];
//[self.view addgesturecognizer:tapGR];
}
-(void)translateView:(UIView*)视图到rect:(CGRect)rect with duration:(NSTimeInterval)duration
{
NSLog(@“将视图转换为rect:%@覆盖:%g”,NSStringFromCGRect(rect),持续时间);
}
-(无效)更新位置
{
if(self.activeTextField&!CGRectIsNull(self.keyboardFrame))
{
UIWindow*window=[[UIApplication sharedApplication]keyWindow];
CGRect localKeyboardFrame=[windowconvertRect:self.keyboardFrame-toView:self.view];
//在键盘和文本字段之间留有最小的空间
CGFloat TEXTFIELDBUSTER=40;
CGRect textFieldFrame=self.activeTextField.frame;
CGRect viewFrame=self.view.frame;
viewFrame.origin.y=0;
如果(CGRectGetMaxY(textFieldFrame)+textFieldBuffer>CGRectGetMinY(localKeyboardFrame))
{
viewFrame.origin.y=-1.0*(CGRectGetMaxY(textFieldFrame)+textFieldBuffer-CGRectGetMinY(localKeyboardFrame));
}
[自翻译视图:self.view到rect:viewFrame,持续时间:0.3];
}
其他的
{
CGRect viewFrame=self.view.frame;
viewFrame.origin.y=0;
[自翻译视图:self.view到rect:viewFrame,持续时间:0.3];
}
}
-(无效)键盘显示:(NSNotification*)通知
{
self.keyboardFrame=[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];