键盘隐藏输入文本

时间:2012-02-28 15:59:37

标签: iphone

我正在here

处理QuickDialog

但是,我的视图无法滚动,键盘正在隐藏输入的文本。到目前为止我所拥有的是 - (void)loadView {

self.navigationController.navigationBarHidden       =   NO;
self.navigationController.navigationBar.tintColor   =   [UIColor blackColor];
self.navigationItem.hidesBackButton                 =   NO;
NSLog(@"LOAD VIEW");

self.root                       =   [[QRootElement alloc] init] ;
self.root.controllerName        =   @"CreateAccountViewController";
self.root.grouped               =   YES;
self.root.title                 =   @"Registration";
[super loadView];

QSection    *section1               =   [[QSection alloc] init];

// Create title cell 
QEntryElement *cell1            =   [[QEntryElement alloc] init];
cell1.title                     =   @"Title: ";
cell1.key                       =   @"title";
cell1.placeholder               =   @"Mr/Mrs/Miss";
cell1.hiddenToolbar             =   YES;
cell1.autocapitalizationType    =   UITextAutocapitalizationTypeNone;
cell1.autocorrectionType        =   UITextAutocorrectionTypeNo;

// Create firstName cell 
QEntryElement *cell2            =   [[QEntryElement alloc] init];
cell2.title                     =   @"FirstName: ";
cell2.key                       =   @"firstName";
cell2.placeholder               =   @"Enter your first name";
cell2.hiddenToolbar             =   YES;
cell2.autocapitalizationType    =   UITextAutocapitalizationTypeNone;
cell2.autocorrectionType        =   UITextAutocorrectionTypeNo;


// Create lastName cell 
QEntryElement *cell3            =   [[QEntryElement alloc] init];
cell3.title                     =   @"LastName: ";
cell3.key                       =   @"lastName";
cell3.placeholder               =   @"Enter your last name";
cell3.hiddenToolbar             =   YES;
cell3.autocapitalizationType    =   UITextAutocapitalizationTypeNone;
cell3.autocorrectionType        =   UITextAutocorrectionTypeNo;

// Create phone number cell 
QEntryElement *cell4          =   [[QEntryElement alloc] init];
cell4.title                   =   @"Phone: ";
cell4.key                     =   @"phoneNumber";
cell4.placeholder             =   @"Enter your phone number";
cell4.hiddenToolbar           =   YES;
cell4.autocapitalizationType  =   UITextAutocapitalizationTypeNone;
cell4.autocorrectionType      =   UITextAutocorrectionTypeNo;
.............................
.............................

[main addElement:cell1];
[main addElement:cell2];
[main addElement:cell3];
[main addElement:cell4];
[main addElement:cell5];
[main addElement:cell6];
[main addElement:cell7];

[self.root addSection:section];

运行之后,我意识到键盘正在隐藏上下文,因为我有很长的输入列表。此外,视图根本不可滚动 here附有图片,您可以看到我的问题...... ![在此输入图像说明] [3]

我通过示例代码,视图是可滚动的,因此键盘根本不会隐藏任何文本。 有任何机构对此问题有任何想法,请帮忙。欢迎所有评论

4 个答案:

答案 0 :(得分:1)

解决此问题的一种方法是将滚动视图的大小调整为[屏幕高度] - [键盘高度](这来自我在某处看到的类似帖子。)

您可能想尝试一下。

答案 1 :(得分:1)

仅仅为了这个记录,自从提出这个问题以来,这已经在QuickDialog中实现了,所以你不必再担心它了。

答案 2 :(得分:0)

有两种可能的方法。

  1. 在键盘打开或关闭时更改视图高度。
  2. 在键盘打开或关闭时更改视图原点。

答案 3 :(得分:0)

在textField的两个委托方法中实现此代码将执行您想要的操作。这是textFieldDidBeginEditing:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
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;
}

UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
    animatedDistance = floor(LANDSCAPE_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];
}

这是textFieldDidEndEditing:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
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];
[self textFieldShouldReturn:textField];

[UIView commitAnimations];
}

如果您想要按钮,可以使用上面的代码设置IBAction。