0

I have a main view controller containing a table view. By clicking the cell I am navigating to the chat screen (loaded with stored messages). Chat screen should be scrolled to the bottom (last message) on load. Now Every time it looks bad scrolling to the bottom. Is there any way to preserve the state of the screen or view controller. So next time when user will visit the screen one should be already scrolled to the bottom?

Am I supposed to cache the view controller? Is this a proper/standard to cache the UIViewController?

I dont want to know how to scroll to the bottom, but want to cache the position state of a view controller/screen.

Mohshin Shah
  • 298
  • 2
  • 13

1 Answers1

0

This is a duplicate of the question: UIScrollView scroll to bottom programmatically

Please find the answer there :) Just set the animation to false in the answer. Ensure the setting is made in init/viewDidLoad method.

Community
  • 1
  • 1
7to4
  • 198
  • 1
  • 8
  • Did you read the question ? I have not questioned for "How to scroll UITableView" – Mohshin Shah Apr 27 '17 at 06:06
  • Scrolling to the bottom works fine. My concern is how to preserve the state. Once it is scrolled to the bottom. Now pop the VC. Now pushing the vc again It should be already scrolled to the bottom. State needs to be preserved. Getting? – Mohshin Shah Apr 27 '17 at 06:08
  • The answer posted may not be exactly what you want, but it's still relevant because it says how to set the content offset. – 7to4 Apr 27 '17 at 06:09
  • So, when you pop, ensure the offset is stored. When pushing again, ensure the stored offset is set. :) Of course, you can's store in the chatVC object, but you should have a [chatScreen:contentOffset] map in the main VC. You can use delegation to store the offset before popping. – 7to4 Apr 27 '17 at 06:10
  • If you require code for this, please let me know. I'll update the answer :) – 7to4 Apr 27 '17 at 06:13
  • Saving the offset wont work. Still you have to apply the offset on loading. right ? It is the same as scrolling to the bottom. No need for offset. We dont want the any other position. we can use contentSize.height rather. But that is not the thing I am asking – Mohshin Shah Apr 27 '17 at 06:17
  • "we can use contentSize.height" This will not address the scenario where the user scrolled half way and wants to return to the same point. I'll tell my understanding of your requirement. 1. We have a chat list screen 2. User goes to chat screen "A" 3. He scrolls down to position "P" 4. He goes back to list screen 5. When he returns to chat screen "A", it should be scrolled to position "P". Kindly correct where I'm wrong :) – 7to4 Apr 27 '17 at 06:20
  • You are not getting the thing. Chat screen should be scrolled to the bottom only once. Suppose on main screen there are 5 items(1,2,3,4,5).And each item is having their chat screen. If I will visit each of them once. And next time they should not scrollToBottom visibly. It must be already scrolled next time. – Mohshin Shah Apr 27 '17 at 06:27
  • And even if we'll save the offset. We need it to be set at any point. ANd that I dont want. Check any chat app. (For eg. WhatsApp) Mine Case: Chat room initially scrolled to the last message. Scrolling is not visibled Your case: Chat room will be loaded where it was left. But not visibly scrolled. – Mohshin Shah Apr 27 '17 at 06:33
  • If you agree, Please update your answer. It is not the duplicate one.:) – Mohshin Shah Apr 27 '17 at 06:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142794/discussion-between-7to4-and-mohshin-shah). – 7to4 Apr 27 '17 at 07:09