Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Setting Color For A DisclosureIndicator Accessory

$
0
0

I have a UITableCell in where the Accessory has to be set as a DisclosureIndicator. In addition to that, I have to follow the style guidelines, set by my designers. It calls for a different color and highlighted color than what what is showing up by setting:

cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

After a little research, it appears that there is no direct way to set what color and highlighted color the accessory is. Or at least I haven't been able to find a way. However, I did find this article that explained how to use a custom UIControl to draw out the DisclosureIndicator using the colors that you choose. You would then set this custom UIControl to cell.AccessoryView and it should draw out.

http://www.cocoanetics.com/2010/10/custom-colored-disclosure-indicators/

So taking a look at the Objective-C code, I decided to give it a shot and convert it to C#. Mind you, I don't know Objective-C, so I may have made a couple of translation mistakes. But, I got something up and compiling. I created my Custom UIControl and set it to the cell.AccessoryView property. But it appears to not be rendering because no accessories show up in my UI. No crashes, nothing. I set a breakpoint on the DrawRect() method, but it appears that this method is not being fired.

Below is my C# code. You can view the original Objective-C code from the link above. So has anyone tried this, or does anyone know why DrawRect() is not being called? It seems no rendering is going on.

public class ColoredDisclosureIndicator : UIControl
    {
        private UIColor _color;
        private UIColor _highlightedColor;

        private ColoredDisclosureIndicator() :base()
        {

        }

        private ColoredDisclosureIndicator(RectangleF frame) : base(frame)
        {
            BackgroundColor = UIColor.Clear;
        }

        private ColoredDisclosureIndicator(UIColor color, UIColor highlightedColor)
        {
            _color = color;
            _highlightedColor = highlightedColor;

        }

        public static ColoredDisclosureIndicator AccessoryWithColor(UIColor color, UIColor highlighted)
        {
            var accessory = new ColoredDisclosureIndicator(new RectangleF(0, 0, 11.0f, 15.0f));
            accessory.Color = color;
            accessory.HighlightedColor = highlighted;

            return accessory;
        }

        public override void DrawRect(RectangleF area, UIViewPrintFormatter formatter)
        {
            base.DrawRect(area, formatter);

            var x = Bounds.GetMaxX() - 3.0f;
            var y = Bounds.GetMaxY();

            const float r = 4.5f;

            var context = UIGraphics.GetCurrentContext();
            context.MoveTo(x - r, y - r);
            context.AddLineToPoint(x, y);
            context.AddLineToPoint(x - r, y + r);
            context.SetLineCap(CGLineCap.Square);
            context.SetLineJoin(CGLineJoin.Miter);
            context.SetLineWidth(3.0f);

            if(Highlighted)
            {
                _highlightedColor.SetStroke();
            }
            {
                _color.SetStroke();
            }

            context.StrokePath();
        }

        public override bool Highlighted
        {
            get
            {
                return base.Highlighted;
            }
            set
            {
                base.Highlighted = value;
                SetNeedsDisplay();
            }
        }

        public UIColor Color
        {
            get
            {
                if(_color == null)
                {
                    _color = UIColor.Black;
                }

                return _color;
            }
            set
            {
                _color = value;
            }
        }

        public UIColor HighlightedColor
        {
            get
            {
                if(_highlightedColor == null)
                {
                    _highlightedColor = UIColor.White;
                }

                return _highlightedColor;
            }
            set
            {
                _highlightedColor = value;
            }
        }
    }

And here is how I try to set it:

public override UITableViewCell GetCell(UITableView tv)
        {
            var cell = base.GetCell(tv);

            cell.SelectionStyle = UITableViewCellSelectionStyle.Gray;
            cell.AccessoryView = ColoredDisclosureIndicator.AccessoryWithColor(UIColor.Black, UIColor.Yellow);

            return cell;
        }

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>