1

I tried using the code below for setting the background for cells. It works fine but it obscures the disclosure indicator. How do I pull the disclosure indicator forward so that it is visible?

cell.backgroundColor = UIColor.clearColor()

let imageView = UIImageView(frame: CGRectMake(0, 0, cell.frame.width, cell.frame.height))
let image = UIImage(named: "sand background 1.png")
imageView.image = image
imageView.alpha = 0.5
cell.addSubview(imageView)
cell.sendSubviewToBack(imageView)
MSeifert
  • 118,681
  • 27
  • 271
  • 293
user1698875
  • 143
  • 1
  • 10

1 Answers1

1

When I do this, the disclosure shows up fine. I'd personally do insertSubview(atIndex: 0) rather than addSubview and sendSubviewToBack if I wanted to manually add the background image view in the view hierarchy manually. Or, I would just set the cell's backgroundView (and selectedBackgroundView). But all of these approaches have the disclosure indicator visible (assuming that the disclosure indicator was turned on) for me. I'd run the app and then use the view debugger and carefully examine the view hierarchy and confirm that the disclosure indicator is really there:

enter image description here

You can then select the debug navigator in the left panel and choose the "View UI Hierarchy" from the button in the top right corner of the debug navigator:

enter image description here

This way you can confirm that the disclosure button is really there, or whether the disclosure indicator is simply not visible (e.g. its color might be too close to your sand-colored background to stand out).


If the problem is that the color of the accessory view is too similar to the background color, you can change the accessory view (see https://stackoverflow.com/a/27971576/1271826). For some accessory types (namely the details or checkmarks), you can change the color simply by changing the tintColor of the tableview. If you want to change the color of the disclosure indicator, you may have to replace the accessory view with a disclosure image that you created yourself, e.g.:

cell.accessoryView = UIImageView(image: accessoryImage)

You can either build this accessory image and add it as an asset to your project, or you can define an accessory image that you build dynamically. For example, here is a yellow disclosure accessory image:

lazy var accessoryImage: UIImage = {
    let rect = CGRect(x: 0, y: 0, width: 10, height: 43.5)
    let insetRect = CGRectInset(rect, 2, 15)
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
    UIColor(red: 1, green: 1, blue: 0, alpha: 1).setStroke()
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: insetRect.origin.x, y: insetRect.origin.y))
    path.addLineToPoint(CGPoint(x: insetRect.origin.x + insetRect.size.width, y: insetRect.size.height / 2.0 + insetRect.origin.y))
    path.addLineToPoint(CGPoint(x: insetRect.origin.x, y: insetRect.size.height + insetRect.origin.y))
    path.lineWidth = 2
    path.stroke()
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}()

That yields:

enter image description here

Community
  • 1
  • 1
Rob
  • 371,891
  • 67
  • 713
  • 902
  • Thanks Rob, I took a look and the disclosure indicator is there. If I set the tableView background colour to clearColor then the disclosure indicator is visible, but black from having a clear background bleeds through the image I set. What settings do you use for the tableView and tableCell view in Storyboard? – user1698875 Dec 24 '15 at 20:24
  • Howzit again Rob, As you suggested the issue is that the colour of the disclosure indicator is too close to the basic colours of the sand image. If I drop the alpha value of the sand image to 0.25, the disclosure indicator is just visible. It is a pity that one cannot set the tint for the disclosure indicator, black would be welcome here! – user1698875 Dec 24 '15 at 21:23
  • You can change the `tintColor` of the table view. – Rob Dec 24 '15 at 21:29
  • Howzit Rob, changing the tintColor of the table view did not change the colour of the disclosure caret, so I just used Plan B and created a custom disclosure indicator. Looks nice now. – user1698875 Dec 25 '15 at 04:39
  • 1
    Yes, the `tintColor` only changes certain types of accessory views (e.g. details button or checkmark, but not disclosure indicator). If you want to change color of disclosure indicator, you have to add it as an image yourself. See end of revised answer. – Rob Dec 25 '15 at 07:00
  • Howzit, Rob. I like the vector drawing better than my Plan B which was to create a caret in Photoshop and import the image, which worked well enough, but vectors are better. Thank you for the help and guidance. You get my upvote! – user1698875 Dec 26 '15 at 17:31