当键盘出现在Objective C中时,向上移动文本框

sas*_*sha 3 keyboard objective-c uiview ios

我是非常初级的移动程序员.我需要在键盘出现时向上移动文本视图.我跟随此移动 - uiview-up-the-the-keyboard-in-ios它运行良好,但我有一个背景图像和我不想向上移动背景图像.所有文本框都嵌入在名为customView的UIView中.我试图向上移动customView而不是self.view.当我开始在第一个textview中输入时,customView向上移动.但是当我移动到第二个textview,customview移动到原始位置,textView成为keyboard.customView需要保持向上移动,当我开始进入第二个textview.我真的很感激任何帮助!

@property (strong, nonatomic) IBOutlet UIView *customView;
 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES; }


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

[self.view endEditing:YES];
return YES; }


- (void)keyboardDidShow:(NSNotification *)notification
{
  //Assign new frame to your view 
    [self.customView setFrame:CGRectMake(0,50,320,460)]; 

}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self.customView setFrame:CGRectMake(0,193,320,460)];
}
Run Code Online (Sandbox Code Playgroud)

Ram*_*rai 7

在viewDidLoad中添加观察者以获得最佳方法.

- (void)viewDidLoad {

    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWillShow:(NSNotification*)aNotification {
    [UIView animateWithDuration:0.25 animations:^
     {
         CGRect newFrame = [customView frame];
         newFrame.origin.y -= 50; // tweak here to adjust the moving position 
         [customView setFrame:newFrame];

     }completion:^(BOOL finished)
     {

     }];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    [UIView animateWithDuration:0.25 animations:^
     {
         CGRect newFrame = [customView frame];
         newFrame.origin.y += 50; // tweak here to adjust the moving position
         [customView setFrame:newFrame];

     }completion:^(BOOL finished)
     {

     }];

    }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
Run Code Online (Sandbox Code Playgroud)