How to change UITableView border color in Xcode

Anybody knows how to change UITableView border color in storyboard. I am using Xcode.

Upvotes: 0

Views: 5331

Answers (4)

Rameswar Prasad
Rameswar Prasad

Reputation: 1331

borderColor on any view(or UIView Subclass) could also be set using storyboard with a little bit of coding and this approach could be really handy if you're setting border color on multiple UI Objects.

Below are the steps how to achieve it,

  1. Create a category on CALayer class. Declare a property of type UIColor with a suitable name, I'll name it as borderUIColor .
  2. Write the setter and getter for this property.
  3. In the 'Setter' method just set the "borderColor" property of layer to the new colors CGColor value.
  4. In the 'Getter' method return UIColor with layer's borderColor.

P.S: Remember, Categories can't have stored properties. 'borderUIColor' is used as a calculated property, just as a reference to achieve what we're focusing on.

Please have a look at the below code sample;

Objective C:

Interface File:

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>

@interface CALayer (BorderProperties)

// This assigns a CGColor to borderColor.
@property (nonatomic, assign) UIColor* borderUIColor;

@end

Implementation File:

#import "CALayer+BorderProperties.h"

@implementation CALayer (BorderProperties)

- (void)setBorderUIColor:(UIColor *)color {
    self.borderColor = color.CGColor;
}

- (UIColor *)borderUIColor {
    return [UIColor colorWithCGColor:self.borderColor];
}

@end

Swift 2.0:

extension CALayer {
var borderUIColor: UIColor {
    set {
        self.borderColor = newValue.CGColor
    }

    get {
        return UIColor(CGColor: self.borderColor!)
    }
}
}

And finally go to your storyboard/XIB, follow the remaining steps;

  1. Click on the View object for which you want to set border Color.
  2. Click on "Identity Inspector"(3rd from Left) in "Utility"(Right side of the screen) panel.
  3. Under "User Defined Runtime Attributes", click on the "+" button to add a key path.
  4. Set the type of the key path to "Color".
  5. Enter the value for key path as "layer.borderUIColor". [Remember this should be the variable name you declared in category, not borderColor here it's borderUIColor].
  6. Finally chose whatever color you want.

Edit: You've to set layer.borderWidth property value to at least 1 to see the border color.

Build and Run. Happy Coding. :)

Upvotes: 1

GenieWanted
GenieWanted

Reputation: 4513

You can probably use User Defined Runtime Attributes to set borderColor or borderWidth through XIB or Storyboard. You will need to select your view and then click on Identity inspector. You will see "User Defined Runtime Attributes" where you can set the borderColor for your tableView.

enter image description here

Upvotes: 1

dopcn
dopcn

Reputation: 4218

Try this in User Defined Runtime Attributes set the key path and value

sample

Upvotes: 1

VRAwesome
VRAwesome

Reputation: 4803

Try following code

#import  "QuartzCore/QuartzCore.h"

tableview.layer.borderWidth = 1.0;
tableview.layer.borderColor = [UIColor lightGrayColor].CGColor;

Upvotes: 0

Related Questions