Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the initial position of UIRefreshControl indicator in iOS App

I am developing iOS App with UITableView and UIRefreshControl.

I would like to change the initial position of UIRefreshControl indicator by 50px down from default. I write down the following code.

However, the initial position is not changed. it remains in its original position.

Could you tell me how to solve this problem?

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];
like image 303
supermonkey Avatar asked Dec 25 '22 04:12

supermonkey


1 Answers

Setting the frame directly doesn't work, but you can still use AutoLayout to put your refreshControl whenever you want. Just don't forget to set translatesAutoresizingMaskIntoConstraints to false.

For example, this code sets the refreshControl in the middle of the screen.

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
like image 165
Senõr Ganso Avatar answered Jan 31 '23 02:01

Senõr Ganso