IT박스

iOS 7 UIRefreshControl tintColor가 beginRefreshing에서 작동하지 않습니다.

itboxs 2020. 10. 22. 07:48
반응형

iOS 7 UIRefreshControl tintColor가 beginRefreshing에서 작동하지 않습니다.


UIRefreshControl (iOS 7에서 빌드)에 tintColor를 설정하려고합니다. 스토리 보드에서 tableViewController에 대한 새로 고침을 활성화 한 다음 ViewController viewDidLoad메서드에서 다음을 수행했습니다.

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

이제 당겨서 새로 고침하면 새로 고침 컨트롤의 색상이 실제로 빨간색입니다.

redSpiny

보기가 표시되면 자동으로 업데이트되기를 원하므로 다음을 수행했습니다.

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

그것은에 따르면, 물레를 보이지 않았다 https://stackoverflow.com/a/16250679/1809736 , 나는 추가

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

강제로 보여줍니다. 표시되지만 이제 기본 색상으로 돌아갑니다.

여기에 이미지 설명 입력

나중에 새로 고치기 위해 수동으로 당기려고하면 빨간색입니다.

iOS6에서 빌드를 시도했는데 정상적으로 작동하므로 iOS7 버그입니까?

추신 : 시뮬레이터의 문제가 아닙니다. 같은 버그를 장치에 구축해 보았습니다.

PPS : 예제 프로젝트를 만들었습니다. 동일한 버그가 있는지 또는 코드에 문제가 있는지 알려 주실 수 있나요? 링크는 다음과 같습니다. http://d.pr/f/pGrV

고마워요!


Hey 방금이 정확한 문제를 발견했습니다.

흥미롭게도 먼저 contentOffset을 설정 한 다음 beginRefreshing을 호출하여 코드를 수정했습니다.

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

이 프로세스를 애니메이션 할 수 있습니다.

[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];
}];

도움이 되었기를 바랍니다.

W


스위프트 솔루션! 에 다음 코드를 삽입하십시오 viewDidLoad.

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

스위프트 3.1

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

@ william-george의 대답은 저를 올바른 방향으로 설정했지만 이상한 자동 레이아웃 애니메이션 문제를 제공했습니다.

그래서 여기 저에게 도움이 된 버전이 있습니다.

- (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];
}

-refresh에 연결된 방법 UIRefreshControl입니다.


이 답변 중 어느 것도 iOS8에서 올바르게 작동하지 않으며 가장 가까운 @jpsim의 답변이지만 페이드 인 애니메이션 중에보기 흉한 검정색 새로 고침 컨트롤을 남겼습니다 (애니메이션 과정에서 검정색과 중간에 교차 페이드 됨 ).

나를 위해 일한 솔루션은 내 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];

그런 다음 프로그래밍 방식으로 UIRefreshControl을 표시하려면 :

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

새로 고침 컨트롤의 높이를 저장해야했습니다. 첫 번째 호출에 대해 설정되어있는 동안 후속 호출의 높이는 0입니다.


tintColor 문제 해결 방법 : viewDidLoad에 추가

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

이제 beginRefresh를 수동으로 호출하면 흰색 표시기가 나타납니다.


빠른:

Swift 및> iOS8을 사용하고 있습니다. 설명 된 대부분의 해결 방법이 저에게 효과적이지 않았습니다. 그것이 내가 작동하는 방법입니다.

viewDidLoad에서 :

customRefreshControl.tintColor = UIColor.clearColor()

다음은 viewDidLoad 안에있을 필요가 없습니다. tableView를 업데이트 할 때마다 호출되는 추가 함수에 넣었습니다.

private func startRefreshControlAnimation() {

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

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

}

이전 답변 중 일부를 결합했습니다. 이것은 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
    })        
}

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()
    }
}

Xamarin (C #)을 사용하여 iOS 용으로 개발했는데 동일한 문제가 발생했습니다.

다음을 설정하여 색상 문제를 해결 AttributedTitle했습니다 RefreshControl.

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");
}

내 업데이트 방법은 다음과 같습니다.

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;
      }
}

일단 ViewDidAppear, 나는 Update프로그래밍 방식으로 호출 합니다. 속성 제목을 설정하기 전에 내 스피너는 검은 색이었습니다. 이제 적절한 붉은 색이 있습니다.

이 '해킹 / 수정'에는 두 번째 버그도 함께 제공된다는 점은 주목할 가치가 있습니다. 처음 새로 고침하면이 AttributedTitle표시되지 않는 것을 알 수 있습니다. 두 번째 (, 세 번째, 네 번째, ...) 시간을 새로 고치면 제목이 제대로 표시됩니다. 그러나 제목이 필요하지 않으면 빈 문자열로 초기화하면됩니다. 이것은 큰 문제가 아닙니다.

나는 이것이 다른 사람들에게 유용 할 수 있기를 바랍니다.


이 해킹은 매우 효과적입니다.

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
        }
    }
}

viewWillAppear에서 UIRefreshControl의 tintColor를 설정해보세요.


몇 가지 해결 방법을 찾았습니다.

 [_TBL setContentOffset:CGPointMake(0,_TBL.contentOffset.y-_refreshControl.frame.size.height) animated:YES];
[_refreshControl performSelector:@selector(beginRefreshing) withObject:nil afterDelay:0.25];
[self getLatestUpdates];

이 문제를 해결 하는 드롭 인 UIRefreshControl + beginRefreshing 범주만들었습니다 .

간단히 말해서 tintColor 문제를 수정하고 tableView가 contentOffset을 수동으로 조정하여 새로 고침 컨트롤이 표시되도록합니다. 시도하십시오 :)


I am working with Xamarin C# (iOS 10) and found that a combination of all of these answers is what fixed it for me.

In my ViewDidLoad I have the following:

    RefreshControl = new UIRefreshControl();
    RefreshControl.TintColor = UIColor.White;
    RefreshControl.ValueChanged += OnRefresh;
    RefreshControl.BackgroundColor = UIColor.Clear;

And later I programmatically call the refresh animation in my ViewDidAppear with the following:

    BeginInvokeOnMainThread(() =>
    {
        UIView.Animate(0, 0.2, UIViewAnimationOptions.BeginFromCurrentState, () =>
        {
            TableView.SetContentOffset(new CGPoint(0, TableView.ContentOffset.Y - RefreshControl.Frame.Size.Height), true);
            RefreshControl.AttributedTitle = new NSAttributedString("");
        },
        () =>
        {
            RefreshControl.BeginRefreshing();
        });
    });

Note the setting of the attributed title and animation block were the parts I was missing for my RefreshControl to take my white tint color.

Thanks to all that have contributed to this question.


This is a bug which occurs when calling beginRefreshing() on the refresh control right after setting its tintColor property (or calling it from viewDidLoad() (details here). There is an easy workaround however, by wrapping the beginRefreshing() call inside a defer statement (Swift 4):

override func viewDidLoad() {
    super.viewDidLoad()
    refreshControl.tintColor = .red
    defer {
        refreshControl.beginRefreshing()
    }
}

When I set

tableView.refreshControl = refreshControl 

refreshControl이 매번 다른 인스턴스 인 경우 여러 번 새로 고침 컨트롤 색상이 항상 검은 색이고 색조 색상을 다른 값으로 설정해도 도움이되지 않는 문제가있었습니다.

tableView.refreshControl = refreshControl한 번만 설정 하고 숨길 필요가있을 때이 스레드에서 더 자세한 내용을 알파 값으로 설정합니다.

UIRefreshControl을 "숨기기"방법은 무엇입니까?


UIView.animateSwift 4에서는 사용 이 작동하지 않았습니다.

내가 사용한 결과는 다음과 같습니다.

extension UIRefreshControl {
    func beginRefreshingManually(with scrollView: UIScrollView, isFirstTime: Bool) {
        if self.isRefreshing { return }

        // Workaround: If we call setContentOffset on the first time that the screen loads
        // we get a black refreshControl with the wrong size.
        // We could just set the scrollView.contentOffset everytime, but it does not animate the scrolling.
        // So for every other time, we call the setContentOffset animated.
        if isFirstTime {
            scrollView.contentOffset = CGPoint(x: 0, y: -self.frame.size.height)
        } else {
            scrollView.setContentOffset(CGPoint(x: 0, y: -self.frame.size.height), animated: true)
        }
        self.beginRefreshing()
    }
}

회전을 시작하기 전에 tableView / scrollView에 대한 콘텐츠 오프셋을 수동으로 설정하십시오.

tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentOffset.y - (refreshControl.frame.size.height)), animated: true)
refreshControl.beginRefreshing()
......

setTintColor가 주 스레드에서 실행되도록합니다. (주 스레드는 UI를 업데이트합니다).

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
    [self.refreshControl setTintColor:[UIColor redColor]];
    [self.refreshControl beginRefreshing];
}];

참고 URL : https://stackoverflow.com/questions/19026351/ios-7-uirefreshcontrol-tintcolor-not-working-for-beginrefreshing

반응형