Search code examples
iosobjective-cuicolor

Objective-C, border color not working with UIColor


I can set the border color using the following code:

[[self.single layer] setBorderColor:[[UIColor colorWithRed:178.0f/255.0f green:178.0f/255.0f blue:178.0f/255.0f alpha:1.0] CGColor];

and it works just fine. But I need to apply this to multiple borders, so I tried the following:

.h

@property (weak, nonatomic) UIColor *appColor;

.m

within viewDidLoad

self.appColor = [UIColor colorWithRed:178.0f/255.0f green:178.0f/255.0f blue:178.0f/255.0f alpha:1.0];

and then later, where the previous color assignment was

[[self.single layer] setBorderColor:[self.appColor CGColor]];

But this does not work - the border comes out black (regardless of what I set for self.appColor). I have also tried [[self.single layer] setBorderColor:self.appColor.CGColor]; with no success. How can I define a UIColor variable just once, but use it to assign border colors in multiple places?


Solution

  • Change this line:

    @property (weak, nonatomic) UIColor *appColor;
    

    To:

    @property (nonatomic, strong) UIColor *appColor;
    

    And see if this helps you.