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

.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 に答える
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 に答える