0

我在编辑UITextField时顶部有一个UIScrollView我看不到键盘。怎么做?

帮我解决这个问题,在此先感谢。

-(void)viewdidload{     
txtsearch =[[UITextField alloc]init];
            [txtsearch setFrame:CGRectMake(410, 35,250, 30)];
            [txtsearch setBorderStyle:UITextBorderStyleRoundedRect];
            txtsearch.placeholder = @"Enter Company/Provider Name";
            txtsearch.text=[NSString stringWithFormat:@"%@",appDelegate.searchFieldName];
            txtsearch.autocorrectionType = UITextAutocorrectionTypeNo;
            txtsearch.font = [UIFont fontWithName:@"Helvetica" size:14];
            txtsearch.delegate=self;
            txtsearch.clearButtonMode = UITextFieldViewModeWhileEditing;
            [testscroll addSubview:txtsearch];


btnSearch = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btnSearch addTarget:self
                          action:@selector(showResults:)
                forControlEvents:UIControlEventTouchDown];
            [btnSearch setBackgroundImage:[UIImage imageNamed:@"search_button_hover.png"] forState:UIControlStateNormal];
            btnSearch.frame = CGRectMake(510, 80, 75, 35.0);
            [testscroll addSubview:btnSearch];

   if(self.tableView)
                    [self.tableView removeFromSuperview];
                self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,210,730,[arr count]*160) style:UITableViewStylePlain];
                self.tableView.delegate=self;
                self.tableView.dataSource=self;
                self.tableView.scrollEnabled = NO;
                [testscroll addSubview:self.tableView];
                [self.tableView reloadData];
                self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.contentSize.height);
                float ftbl = self.tableView.frame.origin.y + self.tableView.contentSize.height + 30;
                testscroll.contentSize=CGSizeMake(768, ftbl);
                testscroll.scrollEnabled=YES;

}
4

2 回答 2

0

这样做,

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField:textField up:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField:textField up:NO];
}



- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait){

        if(up) {
            int moveUpValue = temp.y+textField.frame.size.height;
            animatedDis = 264-(1024-moveUpValue-5);
        }
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
        if(up) {
            int moveUpValue = 1004-temp.y+textField.frame.size.height;
            animatedDis = 264-(1004-moveUpValue-5);
        }
    }
    else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        if(up) {
            int moveUpValue = temp.x+textField.frame.size.height;
            animatedDis = 352-(768-moveUpValue-5);
        }
    }
    else
    {
        if(up) {
            int moveUpValue = 768-temp.x+textField.frame.size.height;
            animatedDis = 352-(768-moveUpValue-5);
        }

    }
    if(animatedDis>0)
    {
        const int movementDistance = animatedDis;
        const float movementDuration = 0.3f; 
        int movement = (up ? -movementDistance : movementDistance);
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        if (orientation == UIInterfaceOrientationPortrait){
            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);       
        }
        else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {

            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
        }
        else if(orientation == UIInterfaceOrientationLandscapeLeft) {

            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
        }
        else {
            baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
        }

        [UIView commitAnimations];
    }
}

参考: iPad出现键盘时如何向上移动视图?

于 2013-02-26T13:25:06.007 回答
0

在您的 .h 文件中添加:

CGFloat animatedDistance;

在 .m 文件中:在 @interface 声明之前添加

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 = 264;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352;

并添加这个 UITextField 委托方法:

- (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];

    [yourScrollView setFrame:viewFrame];

    [UIView commitAnimations];
}

- (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];

    [yourScrollView setFrame:viewFrame];

    [UIView commitAnimations];
}

此外,您可以使用TPKeyboardAvoiding

于 2013-02-26T12:16:27.533 回答