Pergunta

In my iPad application, i am presenting a controller using form sheet style as

controller.modalPresentationStyle=UIModalPresentationFormSheet;

In landscape mode while device's keyboard open i m setting size of tableView so that user can able to see all records of table.

To get event of show/hide keyboard. I have set NSNotification

Problem

But when user tap in textField of table cell using external/virtual keyboard, i m not getting event of keyboard show/hide. So when textfield becomes first responder, Tableview size is decreasing but it's no need while user connected with external keyboard.

Can anyone please guide/help here, what can i do? So that i can stop do set size when using external keyboard.

Register Keyboard Event

- (void)registerForKeyboardNotifications{

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

}

Set Frame While AutoRotate and Text Field Become First Responder

-(void)setFramesOfTable
{

CGRect rct=tableView.frame;
if(appDel.isThisIPad && ([[UIApplication sharedApplication] statusBarOrientation]==UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation]==UIInterfaceOrientationLandscapeRight) && [selectedField isFirstResponder])
{
    rct.size.height=400.0;
}
else
{
    rct.size.height=576.0;
}
tableView.frame=rct;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

selectedField = textField;
[self setFramesOfTable];
}

-(NSUInteger)supportedInterfaceOrientations
{
[self setFramesOfTable];
return UIInterfaceOrientationMaskAll;
}

Thanks.

Foi útil?

Solução

Its not a good idea to change the frame of the table when the text field begins editing. On the iPad, the user can have an external keyboard, the docked keyboard or the split keyboard.

If the user has an external keyboard, you don't need to resize your window. The onscreen keyboard does not appear when using an external keyboard so there is no reason to resize windows.

If the user is using the split keyboard, you don't really need to worry about resizing windows. if they split the keyboard, they could put the keyboard in the middle of the UI, making it impossible (or at very least impractical) to rearrange your UI so its not covered by at least a small portion of the split keyboard. If the user splits the keyboard and covers up important UI components, they need to move the keyboard out of the way.

The best way to resize your UI is in the keyboard will ChangeFrame/Hide methods

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

inside your handlers of these events, you can get the keyboard height, and adjust the UI accordingly

-(void)keyboardWillChangeFrame:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* kbFrame = info[UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];
    BOOL isPortrait = UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
    CGFloat height = isPortrait ? keyboardFrame.size.height : keyboardFrame.size.width;
}

this gets you the animationDuration and the height of the keyboard so that you can use a UIView animateWithDuration block to animate the frame change to your tableview so that it is not obscured by the keyboard.

in keyboardWillHide: you only need to get the animationDuration (the same way as above) from the NSNotification (the height will obviously be 0). Then use another UIView animateWithDuration block to animate your tableview resizing back to its original size

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top