xcode iOS 7 UIRefreshControl tintColor 不适用于 beginRefreshing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19026351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:55:43  来源:igfitidea点击:

iOS 7 UIRefreshControl tintColor not working for beginRefreshing

iphoneobjective-cxcodeuser-interfaceios7

提问by Noé Malzieu

I'm trying to set a tintColor on my UIRefreshControl (building on iOS 7). I enabled refreshing for the tableViewController in storyboard, then in my ViewController viewDidLoadmethod i did the following:

我正在尝试在我的 UIRefreshControl 上设置 tintColor(在 iOS 7 上构建)。我在情节提要中启用了 tableViewController 的刷新,然后在我的 ViewControllerviewDidLoad方法中,我执行了以下操作:

[self.refreshControl setTintColor:[UIColor redColor]];

So now, when I pull to refresh, the color of the refresh control is red indeed:

所以现在,当我拉动刷新时,刷新控件的颜色确实是红色的:

redSpiny

红刺

I want my view to update automatically when it appears, so I did:

我希望我的视图在出现时自动更新,所以我做了:

- (void)viewDidAppear:(BOOL)animated{
    [self.refreshControl beginRefreshing];
}

It didn't show the spinning wheel, according to https://stackoverflow.com/a/16250679/1809736, I added

它没有显示纺车,根据https://stackoverflow.com/a/16250679/1809736,我补充说

[self.tableView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height) animated:NO];

to force show it. It shows it, but now it is back to default color:

强制显示它。它显示它,但现在它恢复为默认颜色:

enter image description here

在此处输入图片说明

If I try manually to pull to refresh afterwards, it is red.

如果我之后尝试手动拉动刷新,它是红色的。

I tried building it on iOS6 and it works as it should, so is that an iOS7 bug?

我尝试在 iOS6 上构建它并且它可以正常工作,那是 iOS7 的错误吗?

P.S.: it is not a problem with the simulator, I tried building it on device, same bug.

PS:这不是模拟器的问题,我尝试在设备上构建它,同样的错误。

P.P.S: I built an example project, can you tell me if you have the same bug or if there is a problem in my code? Here is the link: http://d.pr/f/pGrV

PPS:我构建了一个示例项目,你能告诉我你是否有同样的错误或者我的代码是否有问题?这是链接:http: //d.pr/f/pGrV

Thanks a lot !

非常感谢 !

回答by William George

Hey just stumbled into this exact issue.

嘿,刚刚偶然发现了这个确切的问题。

Interestingly I fixed my code by setting the contentOffset first then calling beginRefreshing

有趣的是,我通过先设置 contentOffset 然后调用 beginRefreshing 来修复我的代码

if(self.tableView.contentOffset.y == 0){
    self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
    [self.refreshControl beginRefreshing];
}

You may want to animate this process:

您可能希望为这个过程设置动画:

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){
    self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
} completion:^(BOOL finished) {
    [self.refreshControl beginRefreshing];
}];

Hope this helps you.

希望这对你有帮助。

W

回答by Fox5150

SWIFT SOLUTION ! Insert the following code in the viewDidLoad:

快速解决方案!在 中插入以下代码viewDidLoad

self.refreshControl.tintColor = UIColor.orangeColor()
self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height)
self.refreshControl.beginRefreshing()

Swift 3.1

斯威夫特 3.1

self.refreshControl.tintColor = UIColor.orange
self.tableView.contentOffset = CGPoint(x:0, y:-self.refreshControl.frame.size.height)
self.refreshControl.beginRefreshing()

回答by jpsim

@william-george's answer set me in the right direction, but was giving me weird autolayout animation issues.

@william-george 的回答让我朝着正确的方向前进,但给我带来了奇怪的自动布局动画问题。

So here's the version that worked for me:

所以这是对我有用的版本:

- (void)programaticallyRefresh {
    // Hack necessary to keep UIRefreshControl's tintColor
    [self.scrollView setContentOffset:CGPointMake(0, -1.0f) animated:NO];
    [self.scrollView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height) animated:YES];
    [self.refreshControl beginRefreshing];
    [self refresh];
}

-refreshis the method tied to the UIRefreshControl.

-refresh是绑定到 的方法UIRefreshControl

回答by Richard Sim

None of these answers are working for me correctly on iOS8, with the closest being @jpsim's answer but that still left an unsightly black refresh control during its fade-in animation (it would cross-fade between black and while over the course of the animation).

这些答案都不适用于 iOS8,最接近的是@jpsim 的答案,但在淡入动画期间仍然留下难看的黑色刷新控件(它会在黑色和动画过程中交叉淡入淡出) )。

The solution that worked for me was to put this immediately after creating the refresh control in my viewDidLoad:

对我有用的解决方案是在我的 viewDidLoad 中创建刷新控件后立即放置:

self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.tintColor = [UIColor whiteColor];
...
self.refreshControlHeight = self.refreshControl.frame.size.height;
[self.tableView setContentOffset:CGPointMake(0, -1) animated:NO];
[self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];

Then to show the UIRefreshControl programmatically:

然后以编程方式显示 UIRefreshControl:

[self.tableView setContentOffset:CGPointMake(0, self.tableView.contentOffset.y-self.refreshControlHeight) animated:YES];
[self.refreshControl beginRefreshing];

I had to store the height of the refresh control, as while it was set for the first invocation, subsequent calls would have a 0 height.

我必须存储刷新控件的高度,因为它是为第一次调用设置的,随后的调用将具有 0 高度。

回答by Sefa Ay?i?ek

Add an extension for UIResfreshControl.

为 UIResfreshControl 添加扩展。

extension UIRefreshControl {
    func beginRefreshingManually() {
        self.tintColor = UIColor.white
        if let scrollView = superview as? UIScrollView {
            scrollView.setContentOffset(CGPoint(x: 0, y:scrollView.contentOffset.y - frame.height), animated: false)
        }
        beginRefreshing()
    }
}

回答by flocbit

SWIFT:

迅速:

I am using Swift and > iOS8. Most of the described workarounds didn't work for me. That's how I got it working:

我正在使用 Swift 和 > iOS8。大多数描述的解决方法对我不起作用。这就是我让它工作的方式:

In viewDidLoad:

在 viewDidLoad 中:

customRefreshControl.tintColor = UIColor.clearColor()

The following doesn't have to be inside viewDidLoad. I put it in an extra function which get's called every time I update the tableView:

以下内容不必在 viewDidLoad 中。我把它放在一个额外的函数中,每次更新 tableView 时都会调用它:

private func startRefreshControlAnimation() {

    self.tableView.setContentOffset(CGPointMake(0, -self.customRefreshControl.frame.size.height), animated: true)

    CATransaction.begin()
    self.customRefreshControl.beginRefreshing()
    CATransaction.commit()

}

回答by Wendy Chu

I combined some of the previous answers. This works for me on iOS 9 and Swift 2:

我结合了之前的一些答案。这在 iOS 9 和 Swift 2 上对我有用:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let contentOffset = self.tableView.contentOffset.y
    UIView.animateWithDuration(0, delay: 0, options: .BeginFromCurrentState, animations: {
        print(self.tableView.contentOffset.y)
            self.tableView.setContentOffset(CGPointMake(0, -self.refreshControl.frame.size.height), animated: false)
        }, completion: { finished in
            self.refreshControl.beginRefreshing()
            self.tableView.setContentOffset(CGPointMake(0, contentOffset/2-self.refreshControl.frame.size.height), animated: true)
            self.refresh() // Code that refresh table data
    })        
}

回答by Upvote

Solution for the tintColor issue: add this in viewDidLoad

tintColor问题的解决方法:在viewDidLoad中添加

[self.refreshControl setTintColor:[UIColor whiteColor]];
[self.refreshControl tintColorDidChange];

Now you have a white indicator when you call beginRefresh manually.

现在,当您手动调用 beginRefresh 时,您有一个白色指示器。

回答by Thomas De Wilde

I develop for iOS using Xamarin (C#) and came across the same issue.

我使用 Xamarin (C#) 为 iOS 开发并遇到了同样的问题。

I fixed the coloring issue, by setting the AttributedTitleof the RefreshControl:

我修复了着色问题,通过设置AttributedTitleRefreshControl

private CGPoint originalOffset;
...
public override void ViewDidLoad ()
{
     base.ViewDidLoad ();
     ...
     originalOffset = TableView.ContentOffset; // Store the original offset of the table view
     RefreshControl = new UIRefreshControl (){ TintColor = UIColor.Red };
     RefreshControl.ValueChanged += ((s,e) => { Update (this, EventArgs.Empty); });
     // Hack so the TintColor of the RefreshControl will be properly set
     RefreshControl.AttributedTitle = new NSAttributedString ("Fetching data");
}

My Update method looks like this :

我的更新方法如下所示:

private async void Update(object sender, EventArgs args)
{
     try {
          TableView.UserInteractionEnabled = false;
          // I find -100 to be a big enough offset
          TableView.SetContentOffset (new CGPoint (0, -100), true);
          RefreshControl.BeginRefreshing ();
          ... // Fetch data & update table source 
          TableView.ReloadData ();
      } catch(Exception) {
          // Respond to exception
      } finally {
          // Put the offset back to the original
          TableView.SetContentOffset (originalOffset, true);
          RefreshControl.EndRefreshing ();
          TableView.UserInteractionEnabled = true;
      }
}

Once the ViewDidAppear, I call Updateprogrammatically. Before setting the attributed title, my spinner would've been black. Now it has the proper red color.

一旦ViewDidAppear,我以Update编程方式调用。在设置属性标题之前,我的微调器会是黑色的。现在它具有适当的红色。

It's worth noticing, that this 'hack/fix' also comes with a second bug. The first time you refresh, you'll notice that the AttributedTitleis not displayed. Refreshing a second (,third,fourth,...) time will display the title properly. But if you don't want a title, you just initialize it with an empty string, and this is not a big issue to you.

值得注意的是,这个“黑客/修复”还带有第二个错误。第一次刷新时,您会注意到AttributedTitle未显示。刷新第二次 (,third,fourth,...) 将正确显示标题。但是如果你不想要一个标题,你只需用一个空字符串初始化它,这对你来说不是什么大问题。

I hope this can be of use to others.

我希望这对其他人有用。

回答by Andrey Gerashchenko

this hack is very working

这个 hack 非常有效

var refreshWasProgramBeginning: Bool = false

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if !refreshWasProgramBeginning {
        UIView.animate(withDuration: 0.25, animations: {
            self.tableView.contentOffset = CGPoint.init(x: 0, y: -self.refreshControl.frame.height)
        }) { (_) in
            self.refreshControl.beginRefreshing()
            self.refreshWasProgramBeginning = true
        }
    }
}