关于ios:当iphone键盘显示时,如何在中心显示我的活动文本字段?

how to show my active textfield in the center when the iphone keyboard shows up?

本问题已经有最佳答案,请猛点这里访问。

除了顶部的文本字段,当我开始输入时,iPhone键盘会隐藏所有剩余的文本字段。如何在键盘上方显示活动的文本字段,然后在键盘隐藏时将其恢复正常?


把你的UITextField放在UIScrollView里,这样使用UIScrollViewcontentOffSet属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == txt1) //
    {
       [txt1 becomesFirstResponder];
       scrMainView.contentOffset = CGPointMake(0, 20);
    }

    else if (textField == txt2) //
    {
       [txt1 resignFirstResponder];
       [txt2 becomesFirstResponder];
       scrMainView.contentOffset = CGPointMake(0, 60);
    }
    //And so on..
    return YES;
}

希望你能得到答案。

谢谢。


在将所有文本字段放入ScrollView之前,请使用以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  #define kOFFSET_FOR_KEYBOARD 80.0

-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:mailTf])
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
    }
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}


- (void)viewWillAppear:(BOOL)animated
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

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

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}


注意观察UIKeyboardWillShowNotificationUIKeyboardWillHideNotification。随这些通知一起提供的NSNotification对象将具有用户信息字典,其中包含键盘完成动画后将具有的帧信息。根据这些通知调整布局,以防止键盘覆盖您想要的内容。


您必须在视图顶部使用滚动视图,并根据您选择的文本字段实现MathematicContentOffset设置。您选择的文本字段可以从委托方法中获得,偏移量的计算可以使用文本字段框完成。