我正在使用UITableView和UIRefreshControl开发iOS App。
我想将UIRefreshControl指标的初始位置从默认值下调50px。我写下以下代码。
但是,初始位置没有改变。它保持原状。
你能告诉我如何解决这个问题吗?
CGFloat customRefreshControlHeight = 50.0f;
CGFloat customRefreshControlWidth = 320.0f;
CGRect customRefreshControlFrame = CGRectMake(0.0f,
customRefreshControlHeight,
customRefreshControlWidth,
customRefreshControlHeight);
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] initWithFrame:customRefreshControlFrame];
refreshControl.tintColor = [UIColor blackColor];
[refreshControl addTarget:self action:@selector(onRefresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
答案 0 :(得分:1)
直接设置框架是行不通的,但是您仍然可以随时使用AutoLayout来放置refreshControl。只是不要忘记将translatesAutoresizingMaskIntoConstraints
设置为false
。
例如,此代码在屏幕中间设置了refreshControl。
let refreshControl = UIRefreshControl()
collectionView.refreshControl = refreshControl
refreshControl.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: refreshControl, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: refreshControl, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
答案 1 :(得分:0)
简单(Swift)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
refreshController.frame = CGRectMake(refreshController.bounds.origin.x,
50.0,
refreshController.bounds.size.width,
refreshController.bounds.size.height);
refreshController.superview?.sendSubviewToBack(refreshController) // for fix overlap tableview
}
答案 2 :(得分:0)
我的解决方案是使用具有覆盖框架属性的自定义UIRefreshControl
类。以下代码适用于所有情况。您可以尝试通过应用变换来实现它而不进行变换。自iOS 11起,无法在viewDidLayoutSubviews
中设置框架。
class OffsetableRefreshControl: UIRefreshControl {
var offset: CGFloat = 64
override var frame: CGRect {
set {
var rect = newValue
rect.origin.y += offset
super.frame = rect
}
get {
return super.frame
}
}
}