How can I set the contentoffset and change the height of the UIScrollview at the same time

I am trying to scroll my UISCrollView when the keyboard is displayed

I use setContentOffset to switch uiview up.

At the same time, I want to reduce the height of my UISCrollView to (view height - keyboard height) so that I can scroll through the entire content view.

I apply as changes to the keyboard notification WillShow

When I actually bring the keyboard to my application, the content is first pushed up and then pushed down (which gives a flickering effect). I am trying to smooth both transforms at a time.

Is it possible?

Code below ---

 - (void) keyboardWillShow { CGPoint contentOffset = scrollView.contentOffset; CGRect scrollViewFrame = scrollView.frame; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; if (contentOffset.y >= 0 && contentOffset.x >= 0) { isContentOffset = YES; contentOffset.y += screenShift; [scrollView setContentOffset:contentOffset animated: NO]; } scrollViewFrame.size.height = keyboardOrigin.y - self.view.frame.origin.y - toolbarHeight; scrollView.frame = scrollViewFrame; [UIView commitAnimations]; } 
+4
source share
4 answers

There is an animation option when you set aContentOffset for an animation. here is the code i use all the time

 - (void)textViewDidBeginEditing:(UITextView *)textView { svos = scrollView.contentOffset; CGRect rect = [textView bounds]; rect = [textView convertRect:rect toView:self.scrollView]; CGPoint point = rect.origin ; point.x = 0 ; [self.scrollView setContentOffset:point animated:YES]; doneButton.enabled = YES; } - (IBAction)donePressed { [scrollView setContentOffset:svos animated:YES]; [textview resignFirstResponder]; doneButton.enabled = NO; } 

This works great for me.

+3
source

Do you replace these changes in the animation block?

 [UIView beginAnimations:@"resizeScroll" context:nil]; // make your changes to set and content offset [UIView commitAnimations]; 
+1
source

I think I have a solution for this. You can handle resizing the view in the textViewDidBeginEditing method. You can simply resize the scrollView frame half to

 CGRect tframe = myscrollView.frame; tframe.size.height/=2; myscrollView.frame = tframe; 

and to initialize or handle the total scroll length, you can set the scroll scroll contentSize of the View just like the frame was set in the above snippet. eg

 CGSize tsize = self.view.frame.size; //here you can change the height and width of the scrollView myscrollView.contentSize = tsize; 

I hope this should do the trick for you. If so, report it.

0
source

Figured out the problem ...

When the keyboard key is pressed, I did startFirstResponder and then re-typed resignFirstResponder on the keyboard. This caused the WillShow keyboard to be accompanied by the WillHide keyboard and another notification of the WillShow keyboard that started the keyboard, returned it, and then again.

Thanks to everyone who tried to help .. and it is a pity that the problem was completely different ...

0
source

Source: https://habr.com/ru/post/1342387/


All Articles