Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload a ui view's content Swift

Tags:

reload

swift

view

I have a view controller which has scroll view in it, and the scroll view has a view. So, view has a lot of buttons and labels which are being showed depending on data which comes from api. And I have a button after pressing it, I want to reload a views (it's content).

like image 535
Jack Avatar asked Aug 04 '17 13:08

Jack


2 Answers

one of the easy hack and NOT the recommended way is to do

self.view.setNeedsLayout()

also you can do self.viewDidLoad() OR self.viewWillAppear(true)

well the different recommended approaches you can use are the following

Delegation KVO Notifications

The other way would be to use a tableView and user reloadData()

Delegation: https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide

KVO: https://cocoacasts.com/key-value-observing-kvo-and-swift-3/

Notifications: http://dev.iachieved.it/iachievedit/notifications-and-userinfo-with-swift-3-0/

like image 145
sarosh mirza Avatar answered Oct 28 '22 16:10

sarosh mirza


The best and proper way is to define a method that will layout each buttons, telling if it should be hidden or display and update its frame.

The easiest but ugly way is to use :

self.viewToReload.setNeedsLayout()
self.viewToReload.layoutIfNeeded()
like image 28
Maxime Avatar answered Oct 28 '22 16:10

Maxime