【发布时间】:2025-12-19 22:30:12
【问题描述】:
场景:包含图像视图的刷新控件。
我确定遇到过这个问题:
- 在 UICollectionView 上创建 UIRefreshControl。
- 水平滚动以注意 UIRefreshControl 是倾斜的。
这是设置代码:
- (void)setupRefreshControl {
self.refreshControl = [UIRefreshControl new];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refresh Data"];
self.refreshControl.tintColor = UIColor.redColor;
self.refreshControl.backgroundColor = UIColor.whiteColor;
UIImage *hook = [UIImage imageNamed:@"Hook"];
UIImageView *hookImageView = [[UIImageView alloc] initWithImage:hook];
UIImage *fish = [UIImage imageNamed:@"Fish"];
UIImageView *fishImageView = [[UIImageView alloc] initWithImage:fish];
UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[hookImageView ,fishImageView]];
stackView.axis = UILayoutConstraintAxisVertical;
stackView.spacing = 10.0;
[self.refreshControl addSubview:stackView];
[stackView setTranslatesAutoresizingMaskIntoConstraints:NO];
UILayoutGuide *container = [_refreshControl layoutMarginsGuide];
[stackView.topAnchor constraintEqualToAnchor:container.topAnchor].active = YES;
CGFloat width = self.refreshControl.bounds.size.width;
[stackView.leftAnchor constraintEqualToAnchor:container.leftAnchor constant:width/2].active = YES;
[self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
self.collectionView.refreshControl = self.refreshControl;
}
如您所见,UIRefreshControl 在水平滚动条上偏离中心。我是否需要始终计算新中心?
必须有一个标准的方法来做到这一点。
【问题讨论】:
标签: ios objective-c uicollectionview uirefreshcontrol