调整键盘可见性的背景

时间:2012-09-21 04:56:00

标签: iphone ios uiscrollview uikeyboard

我在我的应用程序中使用滚动视图,因为当我点击dob文本字段时,datepicker视图显示为弹出窗口,当我在连续文本字段中单击时,视图就像在image,这是我的代码,

日期选择器可见性。

UIDatePicker pop up after UIButton is pressed

用于键盘方向

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[dob resignFirstResponder];
if (txt1.textColor == [UIColor lightGrayColor]) {
    txt1.text = @"";
    txt1.textColor = [UIColor blackColor];
}
if ([textField isEqual:dob])
{
    [self but];
    [dob resignFirstResponder];
    //return NO;
}  
//[self animateTextField:textField up:YES];
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
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.1 * 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];    
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{

//[self animateTextField:textField up:NO];
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];    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];
}

任何人都可以帮我清楚。 enter image description here

3 个答案:

答案 0 :(得分:0)

当键盘出现时,您需要调整滚动视图的大小。

这可以通过添加:

来完成
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardShown) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardHidden) name:UIKeyboardWillHideNotification object:nil];
viewDidLoad中的

。 然后在实现文件中定义keyboardShown和keyboardHidden方法。 当键盘出现并消失时,将自动调用这些方法。 在这些方法中,相应地调整背景视图的大小。因为你正在使用滚动视图,请注意scrollView的contectView大小。因为它与视图的帧大小不同。

答案 1 :(得分:0)

对于滚动视图以及键盘出现时需要移动其内容的基本问题,我发现了open source scroll view class by Michael Tyson to be very useful

基本上,您只需将TPKeyboardAvoidingScrollView类添加到项目中,并在通常使用UIScrollView的位置使用它。如果您愿意,您添加到TPKeyboardAvoidingScrollView的子视图可以是UITextField个对象。当您点击这些字段开始编辑,并且键盘出现时,TPKeyboardAvoidingScrollView容器将选择一个合适的滚动位置,以便键盘不会隐藏正在编辑的字段,并且您不会看到像你有你的屏幕截图。

答案 2 :(得分:0)

我想我遇到了问题。

jst例如,在你的注册页面,许多文本字段和DOB按钮。

所以,基本上在任何文本字段编辑之后,而不是返回键盘,用户按下DOB按钮。

所以你必须首先resignFirstRespoder你所有的文本域。

问题是当你将startEditing框架设置为top而endEditing设置为bottom但是当你点击按钮endEditing没有被调用时所以框架没有设置为bottom。再次,你的startEditing框架再次设置为顶部所以问题aries。

我希望这有帮助...