0

I am developing a chatting application. The application works fine but i have some problem in the UI design.

I have UiTextField in side the UIview, when ever the user want to type a message the keyboard opens, and UIView moves up.

Then when the user press the return button, i want to bring it back to the original position.

I tried but It looks like this

I want it to support for all devices.

enter image description here

Here is my code

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    return YES;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [self.view endEditing:YES];
    return YES;
}


- (void)keyboardDidShow:(NSNotification *)notification
{
    // Assign new frame to your view
    [self.view setFrame:CGRectMake(0,-145,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.

}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self.view setFrame:CGRectMake(0,0,320,460)];
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.tv_Message resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"selected index : %ld", (long)indexPath.row);
}


- (BOOL)textFieldShouldReturn:(UITextField *)aTextField {
    [aTextField resignFirstResponder];
    [self.view setFrame:CGRectMake(0,0,320,460)];
    return YES;
}

Can someone help me to fix this.

syam
  • 799
  • 1
  • 12
  • 30
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77

2 Answers2

1

It seems that you haven't set the trailing constraint of your main view correctly. Then in keyboardDidShow(:) method up your main view dynamically because your view will have height according to device, so make it work on all devices you should move the main view up by calculating the position of textfield and keyboard height and then set the frame of your view accordingly.

1

You can use this link to manage your view according to device. Also you need to set constraints accordingly.

https://stackoverflow.com/a/11282535/6438500

Community
  • 1
  • 1
Sunny
  • 821
  • 6
  • 17