【问题标题】:Move up the textbox when keyboard appears in Objective C当键盘出现在Objective C中时向上移动文本框
【发布时间】:2014-12-26 09:00:31
【问题描述】:

我是一名非常初级的移动程序员。我需要在键盘出现时上移文本视图。我遵循此move-uiview-up-when-the-keyboard-appears-in-ios 并且效果很好,但我有一个背景图像,我不想上移背景图像。所以所有文本框嵌入在名为 customView 的 UIView 中。我试图向上移动 customView 而不是 self.view 。当我开始进入第一个 textview 时,customView 向上移动。但是当我移动到第二个 textview 时,customview 向下移动到原始位置和 textView成为键盘下。当我开始进入第二个文本视图时,customView 需要保持向上移动。我非常感谢任何帮助!

@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)];
}

【问题讨论】:

    标签: ios objective-c uiview keyboard


    【解决方案1】:

    在 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];
    }
    

    【讨论】:

    • 在 ios7 中运行良好,但在 ios8 中无法运行。我不知道为什么。您能帮帮我吗?
    • 在 iOS 8.1 下可以正常使用,不确定 8.0,请在 8.1 中查看。
    • 谢谢。我在stackoverflow.com/questions/26112319/…找到了ios8的解决方案。
    • 这里的自定义视图是什么?
    • @RaviJSS sasha 想要根据软键盘的可见性调整位置的视图。如果没有,可以使用 self.view 代替 customView。
    【解决方案2】:

    // 在主视图上添加滚动视图并在该滚动视图上添加 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;
    }
    

    // 设置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
    }
    

    //将文本框移动到原来的位置

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

    【讨论】:

      【解决方案3】:

      让你的班级实现UITextFieldDelegate

      将以下代码放入viewDidLoad

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

      并在您的 .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;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 2019-10-22
        • 1970-01-01
        • 1970-01-01
        • 2011-12-18
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多