使UIActivityIndi​​cator的大小与UIRefreshControl中的on相同

时间:2015-07-02 14:45:49

标签: ios uiactivityindicatorview uirefreshcontrol

我正在尝试使UIActivityIndi​​cator的大小与UIRefreshControl中的on相同,因为有时我需要手动刷新屏幕,有时数据会在不拉动屏幕的情况下刷新,因此我需要模拟活动指示器。

 _refreshControl = [[UIRefreshControl alloc] init];
    _refreshControl.tintColor = [UIColor grayColor];
    [_refreshControl addTarget:self action:@selector(refreshItems) forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:_refreshControl];


    UIView *activityIndicatorWrapper = [[UIView alloc] initWithFrame:_refreshControl.frame];

    [self.collectionView addSubview:activityIndicatorWrapper];

    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicatorWrapper.frame = _refreshControl.bounds;

    [activityIndicatorWrapper addSubview:_activityIndicator];
    [_activityIndicator startAnimating];
    [_activityIndicator autoCenterInSuperview];
    _activityIndicator.hidesWhenStopped = YES;

问题是尺寸不同,即使我将它们制作成相同的框架。

2 个答案:

答案 0 :(得分:0)

我也尝试过这样做。我不认为这是可能的,因为我们无法看到UIKit在幕后做了什么。

答案 1 :(得分:0)

y: -105改成你想要的,可以先试试y: 0

运行这个然后尝试拉动刷新,希望这有帮助

    private func setupTableViewRefresher() {
        let refreshControl = UIRefreshControl()

        refreshControl.attributedTitle = NSAttributedString(string: " ", attributes: [NSAttributedString.Key.foregroundColor: UIColor.clear, NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 56)])
        refreshControl.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
        refreshControl.backgroundColor = self.view.backgroundColor
        
        // refreshControl.addTarget(self, action: #selector(refreshTableView(_:)), for: .valueChanged)
        
        tableView.refreshControl = refreshControl
    }

    private func createSpinnerFooter() -> UIView {
        let footerView = UIView(frame: CGRect(x: 0, y: -105, width: view.frame.size.width, height: 100))

        let spinner = UIActivityIndicatorView()
        spinner.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        spinner.center = footerView.center
        footerView.addSubview(spinner)
        spinner.startAnimating()
        
        return footerView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
                
        setupTableViewRefresher()

        tableView.isHidden = false
        tableView.tableFooterView = nil

        tableView.tableFooterView = self.createSpinnerFooter()
    }