Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UILabel backgroundColor does not work well #2

Closed
Cubernet opened this issue Aug 25, 2017 · 4 comments
Closed

UILabel backgroundColor does not work well #2

Cubernet opened this issue Aug 25, 2017 · 4 comments

Comments

@Cubernet
Copy link

Hi,

When I tested the demo project on my iPhone6s Plus(10.3.3), I found a bug. You can reproduce the bug in the following steps.

  1. Change ArchiveTableViewController can be selected as single selection in StoryBoard.
  2. Set Title Label's backgroundColor, yellow in light and red in dark.
  3. Add following code in ArchiveTableViewController.swift:
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView.deselectRow(at: indexPath, animated: true)
    }
  1. Run this demo on your iPhone. (It works well on the simulator.)
  2. Touch down one of the cells with two-finger and make it selected, don't touch up. Then one finger pull down and the other finger still touch down on the cell.
  3. Repeat some times, maybe you can see the label bgcolor does not change as we expect.
@Cubernet
Copy link
Author

There are some screenshots.

.light
.dark
.bug

@Cubernet
Copy link
Author

I find another easier way to reproduce the bug. Just don't add the code in step 3, touch any one of the cell and use two-finger change theme. Then you can find we changed the cell's selectedBackgroundView instead of the backgroundView.

@Boerworz
Copy link
Owner

Hi Cubernet!

Thanks for making me aware of this problem! However, it isn't an issue with Gagat per se, but rather with how UITableViewCell modifies the label's background color when highlighted/selected. Basically what I think is happening is the following:

  1. When the cell is displayed we set the label's background color to red.
  2. We tap the cell to select it.
  3. Some code in UIKit stores the current label background color (red) in a variable and changes the label background color to a clear color.
  4. We use Gagat or some other method to change the label's background color to yellow.
  5. We tap another cell to deselect the previous cell.
  6. UIKit restores the label's background color to the color it had before it was selected (red).
  7. The label's background color is now red instead of yellow.

(The same thing seems to happen when highlighting a cell as well)

One way to work around this is to keep track of the current style in the cell, and then always restore the label's background color when the cell is unhighlighted or deselected:

// ArchiveTableViewCell.swift

private var currentStyle: Style?

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)
    if let currentStyle = currentStyle, !highlighted {
        titleLabel.backgroundColor = currentStyle.titleBackgroundColor
    }
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if let currentStyle = currentStyle, !selected {
        titleLabel.backgroundColor = currentStyle.titleBackgroundColor
    }
}

func apply(style: Style) {
    currentStyle = style
    // ...
}

Hopefully this helps you. Let me know if it doesn't and I'll reopen the issue! 🙂

@Cubernet
Copy link
Author

It works and thank you very much!
Before you reply me, I found another way to avoid this bug. Instead of setting the background color of UILabel, I set the UILabel.layer.background , which doesn't affected by highlighted/selected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants