objective c:如果单击textfield或textview,则在键盘出现时向上移动视图

时间:2015-11-24 10:54:02

标签: ios objective-c

当我选择某个textField或textview而不是我拥有的每个文本字段时,我试图让我的视图向上移动。该视图有4个文本字段和一个textview。我只希望第四个文本字段和textview在键盘激活时向上移动。 (因为键盘覆盖了textfield和textview) 我在这里尝试了许多建议,但它们都不适合我。

我找到了一个示例(https://stackoverflow.com/a/21257034/5292561)我希望它看起来如何,但是当点击每个文本字段时,视图向上移动而不是当我只点击第4个文本字段或textview时。

注意:我想避免使用scrollview,因为我不喜欢它的使用方式。我读过有关它的负面消息。

我还在努力学习,请耐心等待。 提前致谢

以下是textField和Textview的代码,我想在单击textfield和textview时向上移动:

//dienstPicker input and styling

self.dienstPicker = [[UIPickerView alloc] init];
self.dienstPicker.delegate = self;
self.dienstPicker.dataSource = self;
self.dienstPicker.showsSelectionIndicator = YES;

[self.dienstPicker selectRow:1 inComponent:0 animated:YES];
//UITextField *textfield = [UIResponder currentFirstResponder];
[self.dienstTextField setInputView:_dienstPicker];
UIToolbar *toolBar2=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar2 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn2=[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                           style:UIBarButtonItemStylePlain
                                                          target:self
                                                          action:@selector(ShowSelectedOption2)];

UIBarButtonItem *space2= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                       target:nil
                                                                       action:nil];
[toolBar2 setItems:[NSArray arrayWithObjects:space2, doneBtn2, nil]];
[self.dienstTextField setInputAccessoryView:toolBar2];

self.dienstArray = @[@"test1",@"test2"];

    self.omschrijvingTextView.delegate = self;
    self.omschrijvingTextView.layer.borderWidth = 1.0f;
    self.omschrijvingTextView.layer.borderColor = [[UIColor lightGrayColor] CGColor];

UIToolbar *toolBar4=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar4 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn4=[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                           style:UIBarButtonItemStylePlain
                                                          target:self
                                                          action:@selector(ShowDescription)];
UIBarButtonItem *space4= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                       target:nil
                                                                       action:nil];
[toolBar4 setItems:[NSArray arrayWithObjects:space4, doneBtn4, nil]];
[self.omschrijvingTextView setInputAccessoryView:toolBar4];

CGRect textViewFrame = CGRectMake(0, 0, 320, 44);
UITextView *omschrijvingTextView = [[UITextView alloc] initWithFrame:textViewFrame];
omschrijvingTextView.returnKeyType = UIReturnKeyDone;

pragma mark - UIPickerViewDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if ([pickerView isEqual: _dienstPicker]){
        return [self.dienstArray count];
    } else if ([pickerView isEqual: _projectPicker]){
        return [self.projectArray count];
    } else return 0;
}

pragma mark - UIPickerViewDelegate

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if ([pickerView isEqual: _dienstPicker]){
        return self.dienstArray[row];
    } else if ([pickerView isEqual: _projectPicker]){
        return self.projectArray[row];
    } else return 0;
}

-(void)pickerView:(UIPickerView *)pickerView selectRow:(NSInteger)row inComponent:(NSInteger)component {
    if ([pickerView isEqual: _dienstPicker]){
        self.dienstTextField.text = self.dienstArray[row];
    } else if ([pickerView isEqual: _projectPicker]){
        self.projectTextField.text = self.projectArray[row];
    }
}

4 个答案:

答案 0 :(得分:2)

在Viewcontroller.h中

@interface ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>
{
 CGFloat animatedDistance;

 }
ViewController.M中的

将以下4行粘贴在类的任何位置(方法外)

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
委托方法中的

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
  if(textField.tag==3) //textFieldFirst.tag =0,textFieldSecond.tag=1,textFieldThree.tag=2,textFieldFour.tag=3
  {
    CGRect textFieldRect =
    [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect =
    [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)
    {
       heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
       heightFraction = 1.0;
    }


    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
 }
}

- (void)textFieldDidEndEditing:(UITextField *)textField

{
   if(textField.tag==3)
   {
     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
   }

 }

-(void)textViewDidBeginEditing:(UITextField *)textView
{
  CGRect textViewRect =
  [self.view.window convertRect:textView.bounds fromView:textView];
  CGRect viewRect =
  [self.view.window convertRect:self.view.bounds fromView:self.view];
  CGFloat midline = textViewRect.origin.y + 0.5 * textViewRect.size.height;
  CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
  CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
  CGFloat heightFraction = numerator / denominator;
  if (heightFraction < 0.0)
  {
    heightFraction = 0.0;
  }
  else if (heightFraction > 1.0)
  {
    heightFraction = 1.0;
  }
  animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y -= animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];
}

- (void)textViewDidEndEditing:(UITextField *)textView
{
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y += animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];
}

答案 1 :(得分:0)

如果您在滚动视图中实现了文本字段,并在使用键盘管理内容时将其移动为苹果建议。您可以将标记分配给文本字段,并在单击特定文本字段时通过检查文本字段的标记来执行滚动移动。希望这有帮助

答案 2 :(得分:0)

有很多方法可以实现这一目标。一种简单的方法是为每个textField分配标签,而不是在委托方法

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
   Int *tagId = textView.tag;
if (tagId == "TAG OF YOUR TEXTFIELD") {
    // Move View up
    return YES;
 }
  return NO;
}
//Also Move your view down in textViewShouldEndEditing method.

答案 3 :(得分:0)

  1. 如果您使用的是故事板,请将所有textfields放入ScrollView并在 ViewController.m
  2. 上使用此代码
  3. 不要忘记将textfields delegates设置为ViewController(通过情节提要或通过ViewDidLoad上的代码
  4. -(void)textFieldDidBeginEditing:(UITextField *)textFieldView {
    if (textFieldView == self.myTextField)
        {
            CGPoint scrollPoint = CGPointMake(0.0, self.myTextField.frame.origin.y - self.myTextField.frame.size.height/2);
            [myScrollView setContentOffset:scrollPoint animated:YES];
        }
    }
    
    -(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
        if (textField == self.myTextField)
        {
             CGPoint scrollPoint;
             [_email resignFirstResponder];
             scrollPoint = CGPointZero;
             [myScrollView setContentOffset:scrollPoint animated:YES];
        }
        return YES;
    }