简体   繁体   English

SVPullToRefresh自定义视图导致layoutIfNeeded进入无限循环

[英]SVPullToRefresh custom view causes layoutIfNeeded go into infinite loop

I tried using custom views for the SVPullToRefresh but I've stumbled upon weird issue. 我尝试对SVPullToRefresh使用自定义视图,但偶然发现了一个奇怪的问题。 I've made the demo project to illustrate the issue: https://github.com/gaks/SVRefreshProblem 我已经完成了演示项目来说明问题: https : //github.com/gaks/SVRefreshProblem

The project is simply one UITableViewController with a TableView and the xib file with custom refresh bar view. 该项目只是一个带有TableView的UITableViewController和带有自定义刷新栏视图的xib文件。 The SVPullToRefresh is added to the tableView via the IBOutlet in the viewDidLoad: https://github.com/gaks/SVRefreshProblem/blob/master/SVRefreshProblem/DemoViewController.m SVPullToRefresh通过viewDidLoad中的IBOutlet添加到tableView: https : //github.com/gaks/SVRefreshProblem/blob/master/SVRefreshProblem/DemoViewController.m

There is also a code that loads the RefreshBarView from the xib file and sets it as a custom view for SVPullToRefresh (for 'loading' state only for illustration): 还有一个代码可从xib文件中加载RefreshBarView并将其设置为SVPullToRefresh的自定义视图(仅用于“加载”状态以供说明):

RefreshBarView* refreshBarView = [[[NSBundle mainBundle] loadNibNamed:@"RefreshBarView" owner:self options:nil] objectAtIndex:0];
refreshBarView.bounds = CGRectMake(0, 0, 320, 60);

[[tableView pullToRefreshView] setCustomView:refreshBarView forState:SVPullToRefreshStateLoading];

The issue is that when you pull the table view to trigger the refresh it goes into infinite loop and eat-all-available-memory state after calling [self layoutIfNeeded] in the - (void)setState:(SVPullToRefreshState)newState . 问题是,当您拉表视图以触发刷新时,它将在- (void)setState:(SVPullToRefreshState)newState调用[self layoutIfNeeded]后进入无限循环并吞噬所有可用内存状态。 As far as I was able to debug it keeps calling - (void)layoutSubviews over and over. 据我所能调试的,它不断地反复调用- (void)layoutSubviews

What's weird is that when you open the xib file: https://github.com/gaks/SVRefreshProblem/blob/master/SVRefreshProblem/SVPullToRefresh/RefreshBarView.xib ... and remove the label from the view - it works just fine (except you have an empty view). 奇怪的是,当您打开xib文件时: https : //github.com/gaks/SVRefreshProblem/blob/master/SVRefreshProblem/SVPullToRefresh/RefreshBarView.xib ...并从视图中删除标签-效果很好(除非您有一个空白视图)。

What am I doing wrong? 我究竟做错了什么?

Loading from nibs is for ViewControllers, not views proper. 从笔尖加载是针对ViewControllers的,不适用于正确的视图。 You can try to do this programmatically which will work : 您可以尝试以编程方式执行此操作,该方法将起作用:

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    [v setBackgroundColor:[UIColor grayColor]];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    [label setText:@"asdf"];
    [v addSubview:label];
    [[tableView pullToRefreshView] setCustomView:v forState:SVPullToRefreshStateLoading];

尝试在笔尖中关闭“使用自动布局”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM