DGElasticPullToRefresh icon indicating copy to clipboard operation
DGElasticPullToRefresh copied to clipboard

Unable to simultaneously satisfy constraints

Open enricopiovesan opened this issue 8 years ago • 25 comments

Hey, I try to use it whit the storyboard, but when I pull down the table view, the tableView goes away. Probably, because I use a tableView in Storyboard with constraints.. I don't know if you know what i meen :)

enricopiovesan avatar Oct 20 '15 20:10 enricopiovesan

Hey! Could you please show me video? Could you also try on an empty project to make sure it is because of constraints? I will have a look tomorrow as well.

gontovnik avatar Oct 20 '15 21:10 gontovnik

I tried to create an empty project: A simple viewController with inside a TableView

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var testTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let loadingView = DGElasticPullToRefreshLoadingViewCircle()
        loadingView.tintColor = UIColor(red: 78/255.0, green: 221/255.0, blue: 200/255.0, alpha: 1.0)
        testTableView.dg_addPullToRefreshWithActionHandler({ [weak self] () -> Void in
            // Add your logic here
            // Do not forget to call dg_stopLoading() at the end
            self?.testTableView.dg_stopLoading()
            }, loadingView: loadingView)
        testTableView.dg_setPullToRefreshFillColor(UIColor(red: 57/255.0, green: 67/255.0, blue: 89/255.0, alpha: 1.0))
        testTableView.dg_setPullToRefreshBackgroundColor(testTableView.backgroundColor!)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController: UITableViewDataSource {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 30
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "cellIdentifier"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)

        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
            cell!.contentView.backgroundColor = UIColor(red: 250/255.0, green: 250/255.0, blue: 251/255.0, alpha: 1.0)
        }

        if let cell = cell {
            cell.textLabel?.text = "\(indexPath.row)"
            return cell
        }

        return UITableViewCell()
    }

}

// MARK: -
// MARK: UITableView Delegate

extension ViewController: UITableViewDelegate {

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

}

here you can see the bug, when I pull down

ezgif com-video-to-gif

enricopiovesan avatar Oct 21 '15 00:10 enricopiovesan

Wow, I see. I will test it today with storyboards.

gontovnik avatar Oct 21 '15 06:10 gontovnik

Ok, I found what is the issue. Now I need to find out what is the best approach to fix that. Basically, I use this trick to force stop table view scrolling:

func dg_stopScrollingAnimation() {
    if let superview = self.superview, let index = superview.subviews.indexOf({ $0 == self }) as Int! {
        removeFromSuperview()
        superview.insertSubview(self, atIndex: index)
    }
}

As we use weak references for storyboard items it becomes deallocated when we call "removeFromSuperview".

gontovnik avatar Oct 21 '15 10:10 gontovnik

I'm having the same problem. Were you able to find a way to fix this issue?

jyounus avatar Oct 26 '15 16:10 jyounus

There is one way to fix this issue, but it may cause other issues. Just comment out all in this func:

func dg_stopScrollingAnimation() {
//  if let superview = self.superview, let index = superview.subviews.indexOf({ $0 == self }) as Int! {
//    removeFromSuperview()
//    superview.insertSubview(self, atIndex: index)
//  }
}

gontovnik avatar Oct 26 '15 16:10 gontovnik

I would recommend to test first.

gontovnik avatar Oct 26 '15 16:10 gontovnik

Just tried it and it works, but there's a "flicker" problem now. Sometimes my header view on my UITableView jumps for a split second and then works as it should do. Not ideal, any ideas why the header view might cause problems?

jyounus avatar Oct 26 '15 16:10 jyounus

To tell the truth, I did not test with a header view. It can actually be because of that commented stuff. If you have time, would be great if you could test with a header view but implement it programatically, without storyboard. If not, please let me know, I will do it midnight

gontovnik avatar Oct 26 '15 16:10 gontovnik

I'm currently creating the header view programatically, here's the code for it. Only the tableView is laid out via Storyboard, but not the header view.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let h: CGFloat = self.tableView(tableView, heightForHeaderInSection:0)
        let text = self.tableView(tableView, titleForHeaderInSection: 0)
        let fullFrame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: h)

        let label = UILabel(frame: fullFrame)
        label.font = ..
        label.text = ..
        label.textColor = ..
        label.textAlignment = .Center

        let view = UIView(frame: fullFrame)
        view.addSubview(label)
        view.backgroundColor = ..

        return view
    }

jyounus avatar Oct 26 '15 17:10 jyounus

Hey, did you get a chance to look into this?

jyounus avatar Oct 29 '15 12:10 jyounus

I had the same problem. I took a look at dg_stopScrollingAnimation, it appears that selfis not deallocated but this code deletes all the constraints:

removeFromSuperview()
superview.insertSubview(self, atIndex: index)

Then the autolayout error makes the tableView disappear.

As you said the problem is solved by commenting the function and I did not find side effects yet.

tgyhlsb avatar Nov 02 '15 11:11 tgyhlsb

Let's see, if you do not face any problems with commenting it out - I will remove it and update cocoapods. The purpose of that was to fix one issue when pull to refresh is loading and user tries to scroll quickly up.

gontovnik avatar Nov 02 '15 11:11 gontovnik

Ok, I did not succeed to make that happen. But I trust you ;)

Another clean way to fix this (without updating pod manually or forking it):

extension UIScrollView {
    func dg_stopScrollingAnimation() {}
}

tgyhlsb avatar Nov 02 '15 11:11 tgyhlsb

Thanks :) will update git if all perfectly works

gontovnik avatar Nov 02 '15 12:11 gontovnik

I got this bug too :(

b992 avatar Nov 03 '15 15:11 b992

Have a quick look on the comments above, there is a potential fix for that

gontovnik avatar Nov 03 '15 15:11 gontovnik

I got the same problem when using auto layout and this

extension UIScrollView {
    func dg_stopScrollingAnimation() {}
}

Fix the issue, but not sure whether it will cause other issue

honghaoz avatar Nov 11 '15 04:11 honghaoz

any news guys?

enricopiovesan avatar Dec 09 '15 10:12 enricopiovesan

removeFromSuperview() indeed removes all constraints, I just saw this problem occurred on my app too.

If you just want to stop the scrolling, why not call

self.setContentOffset(self.contentOffset, animated: false)

instead? Does this serve the same purpose?

joe528 avatar Jan 16 '16 04:01 joe528

@joe528 I think that method to stop scrolling is not needed at all. It just needs more testing to confirm that.

gontovnik avatar Feb 08 '16 15:02 gontovnik

Hi all. First, congratulations to @gontovnik for this library! is awesome ! any news about this bug? Thanks

juan-sanzone-olx avatar Mar 08 '16 01:03 juan-sanzone-olx

extension UIScrollView { func dg_stopScrollingAnimation() {} }

fix the issue for me, but it flickers for first time only, Any workaround for this too ?

GauravTechbirds avatar Mar 09 '16 06:03 GauravTechbirds

Could this be related to auto layout universally? If you remove the tableView from it's superview all constraints are also removed. And when it is re-added it has no constraints except ones that are created automatically. That is what is creating the error that is logged.

I recently just had this issue and I had not used Storyboards. I created all constraints programmatically. Also I am not seeing any flicker and the library does work after using the fix above.

k3zi avatar Apr 30 '16 03:04 k3zi

As dg_stopScrollingAnimation was deprecated, it seems like leading to this issue(https://github.com/gontovnik/DGElasticPullToRefresh/issues/47),so I have to bring dg_stopScrollingAnimation back and modify it, but I can't understand why deprecate it can lead this issue.

LvJianting avatar Jul 18 '16 13:07 LvJianting