What is the UIColor of the default UITableView separator?

31,597

Solution 1

… in terms of CGContextSetRGBStrokeColor it should be:

CGContextSetRGBStrokeColor (
   CGContextRef c,
   224.0/255.0,
   224.0/255.0,
   224.0/255.0,
   CGFloat alpha
);

Quite simple and hopefully a solution for your problem.

Solution 2

The color is not guaranteed to be a specific color. It can be changed over OS and SDK versions. You can retrieve exact color dynamically by accessing separatorColor property.

UITableView* TV = [[UITableView alloc] init];
UIColor* C = [TV separatorColor];
CGColorRef CGC = [C CGColor];

Now you can get the each channel values through UIColor's methods. Or use the CGColor directly for drawing.

Here's header file comment of the property in UITableView.h.

@property(nonatomic,retain) UIColor *separatorColor;
// default is the standard separator gray

If you want to avoid instantiation cost of UITableView for each time, just get it once and cache it.


As @Isuru noted in comment, you can write in Swift like this.

UITableView().separ‌​atorColor

As @Jordan noted in comment, you also can store the result to avoid further evaluation cost.

let defaultTableSeparato‌​rColor = UITableView().separa‌​torColor

Solution 3

It seems it changed for iOS 7:

Now the colour is RGB(200, 199, 204):

[UIColor colorWithRed:200/255.0 green:199/255.0 blue:204/255.0 alpha:1.0];

And don't forget the proper line height is 1 px. The code for creating corresponding UIView:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 1/[[UIScreen mainScreen] scale])];
view.backgroundColor = [UIColor colorWithRed:200/255.0 green:199/255.0 blue:204/255.0 alpha:1.0];

Solution 4

Swift 3

Just set to nil to revert to default.

tableView.separatorColor = nil

Solution 5

[UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1.0];
Share:
31,597

Related videos on Youtube

Alejandra
Author by

Alejandra

Updated on September 21, 2020

Comments

  • Alejandra
    Alejandra over 3 years

    Can anyone tell me the UIColor name or exact RGBA for the default iPhone UITableView separator?

    It looks like a light gray color, but it's not [UIColor lightGrayColor]; it's lighter than that.

  • Alejandra
    Alejandra almost 13 years
    What is that in terms of CGContextSetRGBStrokeColor (floats)?
  • Jason
    Jason over 11 years
    Not sure why this hasn't got more upvotes, perfect solution imo :)
  • pronebird
    pronebird almost 10 years
    @Jason because it's expensive if you just need a color.
  • eonil
    eonil almost 10 years
    @Andy This is (or maybe, and hopefully was) the only way to get semantically correct color. Others are all semantically wrong. Though most people just can satisfy with approximated result, but still it doesn't mean that's correct.
  • pronebird
    pronebird almost 10 years
    @Eonil too bad separatorColor is not UIAppearance compliant, otherwise it might be easier to get. I agree with you regarding approximation, but you might not even notice any difference on screen. In a long shot your solution is next OS ready if Apple changed a color scheme.
  • eonil
    eonil almost 10 years
    @Andy Yeah, I agree that color change is unlikely to happen, and the app would be updated before the SDK changes.
  • Isuru
    Isuru over 9 years
    This is neat. In iOS 8 with Swift, you can just get this in one line without instantiating. UITableView().seperatorColor.
  • d370urn3ur
    d370urn3ur almost 9 years
    @Isuru when you call UITableView() you are instantiating a UITableView
  • Jordan Smith
    Jordan Smith over 7 years
    To add to the swift answer... since global properties are lazily computed, at the top of the file you need to use it, just use a one line, auto cached value: private let defaultTableSeparatorColor = UITableView().separatorColor
  • Scott
    Scott over 7 years
    To be more approximate, in iOS 9 it is 217.0f/255.0f for R, G & B.
  • Nike Kov
    Nike Kov over 6 years
    Best answer here!