How to reload a ui view's content Swift

48,121

Solution 1

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/

Solution 2

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()
Share:
48,121
Jack
Author by

Jack

Updated on October 15, 2020

Comments

  • Jack
    Jack over 3 years

    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).