How to reload a ui view's content Swift

白昼怎懂夜的黑 提交于 2021-01-17 16:15:50

问题


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


回答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/




回答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()


来源:https://stackoverflow.com/questions/45508232/how-to-reload-a-ui-views-content-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!