Regolazione dell'interfaccia quando appare la tastiera per UITextField o UITextView

StackOverflow https://stackoverflow.com/questions/402658

  •  03-07-2019
  •  | 
  •  

Domanda

Ho una tabella con ogni cella contenente un'etichetta e un campo di testo. Il problema è che quando vado a modificare l'ultima riga, la tastiera nasconde la parte inferiore della tabella e non riesco a vedere cosa viene digitato. Come posso spostare la mia interfaccia sopra la tastiera in modo da vedere cosa viene digitato?

Grazie, Mustafa

È stato utile?

Soluzione

Ti consigliamo di registrare il tuo viewController per gli eventi UIKeyboardDidShowNotification e UIKeyboardWillHideNotification . Quando li ottieni, dovresti adattare i limiti del tuo tavolo; la tastiera ha un'altezza di 170 pixel, quindi riduci o aumenta i limiti del tavolo in modo appropriato e dovrebbe adattarsi correttamente alla tastiera.

Altri suggerimenti

Questo problema è complesso a seconda dello scenario dell'interfaccia utente. Qui discuterò uno scenario in cui UITextField o UITextview risiede in un UITableViewCell.

  1. È necessario utilizzare NSNotificationCenter per rilevare l'evento UIKeyboardDidShowNotification. vedi http://iosdevelopertips.com/user-interface/adjust -textfield-nascosto-by-keyboard.html . È necessario ridurre le dimensioni del riquadro UITableView in modo che occupi solo l'area dello schermo che non è coperta dalla tastiera.

    1. Se si tocca un UITableViewCell, il sistema operativo posizionerà automaticamente la cella nell'area di visualizzazione di UITableView. Ma non succede quando si tocca un UITextView o UITableViewCell anche se risiede in un UITableViewCell.

Devi chiamare

[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 

per programmare " tocca " la cellula.

Se si implementano entrambi i punti, vedrai la posizione UITextView / Field proprio sopra la tastiera. Tieni presente che UITableViewCell in cui risiede UITableView / Field non può essere più alto di "non coperto". la zona. Se questo non è il tuo caso, esiste un approccio diverso, ma non discuterò qui.

Assicurati solo di avere abbastanza spazio di scorrimento al di sotto di quello. Perché, per quanto ne so, l'iPhone si regola automaticamente e mostra la casella di testo focalizzata nel momento in cui appare la sua tastiera.

Rimuovere / commentare le righe in cui viene modificata l'altezza corretta sembra risolvere il problema. Grazie.

Codice modificato:

# define kOFFSET_FOR_KEYBOARD 150.0     // keyboard is 150 pixels height

// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;

if (movedUp) {
    // If moving up, not only decrease the origin but increase the height so the view 
    // covers the entire screen behind the keyboard.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    // rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
    // If moving down, not only increase the origin but decrease the height.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    // rect.size.height -= kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;
[UIView commitAnimations];

}

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasHidden:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
        keyboardVisible = NO;

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardVisible )
        return;

    if( activeTextField != MoneyCollected)
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-300;
        frame.size.height += keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardVisible = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) 
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-300;
        frame.size.height -= keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardVisible = NO;
}
tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

prova questo mio codice questo ti aiuterà

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top