我正在使用 UITableView 和 UIRefreshControl 开发 iOS 应用。
我想将 UIRefreshControl 指示器的初始位置从默认值向下更改 50 像素。我写下以下代码。
但是,初始位置没有改变。它保持在原来的位置。
你能告诉我如何解决这个问题吗?
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];
最佳答案
直接设置框架是行不通的,但您仍然可以使用 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
关于ios - 更改 iOS App 中 UIRefreshControl 指示器的初始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25673380/