简体   繁体   English

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

[英]Move up the textbox when keyboard appears in Objective C

I am very junior mobile programmer .I need to move up text views when keyboard appears.I follows this move-uiview-up-when-the-keyboard-appears-in-ios and it works well but I have a background image and I do not want to move up background image .so all textboxes are embed in UIView named as customView.I tried to move up customView instead of self.view .When I start enter in first textview, the customView moves up.But when I move to second textview,customview moves down to original position and textView become under the keyboard.customView need to stay move up the when i start enter in second textview .I really appreciate any help!. 我是非常初级的移动程序员。我需要在键盘出现时向上移动文本视图。我跟随此移动 - 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)];
}

Add the observer in viewDidLoad for best approach. 在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];
}

// Add a scrollview on main view and add UITextField on that scrollview //在主视图上添加scrollview并在该scrollview上添加UITextField

-(void) viewDidLoad
{
  UIScrollView  *myScrollView =  [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  myScrollView.contentSize = CGSizeMake(320, 500);
  myScrollView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
  [self.view addSubview:myScrollView];

  UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,30,100,33)];
  [myScrollView addSubview:myTextField];
  myTextField.delegate = self;
}

// Set the scrollview content offset to make the myTextField move up //设置scrollview内容偏移量以使myTextField向上移动

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[myScrollView setContentOffset:CGPointMake(0,textField.center.y-80)           animated:YES]; 
 // here '80' can be any number which decide the height that textfiled     should move
}

//To move the textfield to its original position //将文本字段移动到其原始位置

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
  [[myScrollView  setContentOffset:CGPointMake(0,0) animated:YES];
  [textField resignFirstResponder];
  return YES;
}

Make your class implement the UITextFieldDelegate . 让您的类实现UITextFieldDelegate

Put the following code in viewDidLoad . 将以下代码放在viewDidLoad

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

And define the following functions in your .m file. 并在.m文件中定义以下函数。

- (void)keyboardWasShown:(NSNotification *)aNotification

{// scroll to the text view
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible.
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
self.scrollView.scrollEnabled = YES;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
// scroll back..
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
self.scrollView.scrollEnabled = NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM