Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UIButton Layer Border Width and Color in Interface Builder

Can I use IB_DESIGNABLE and/or IBInspectable to set layer.borderWidth and layer.borderColor in Interface Builder? I am currently creating my button in code but I'd like to be able to set all of this in IB but I'm not sure if these properties can be set that way in Xcode 6. I'd like to make this an IBOutlet instead of having all of this set in code. Here is my button code now.

directions = [UIButton buttonWithType:UIButtonTypeRoundedRect];
directions.titleLabel.textAlignment = NSTextAlignmentCenter;
directions.titleLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:20.0];
[directions setTitle:@"Directions" forState:UIControlStateNormal];
[directions setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
directions.frame = CGRectMake(20, 178, 70, 70);
directions.layer.borderWidth = 2.0f;
directions.layer.borderColor = [UIColor whiteColor].CGColor;
directions.clipsToBounds = YES;
directions.backgroundColor = [UIColor clearColor];
[directions addTarget:self action:@selector(getDirections:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:directions];

I set these values as suggested and the border is never shown in the simulator. EDIT: I found out why the border wasn't showing up when setting these values in IB. The border color is a CGColor so I had to set it in code.

like image 593
raginggoat Avatar asked Oct 02 '14 13:10

raginggoat


People also ask

How do I add a border to UIButton storyboard?

Border Color you can set your UIButton User Defined Run Time Attributes ie borderWidth, cornerRadius, borderColor etc. As shown in the image. Note:- don't use layer. before the attribute name, it will not work.

How do you change the border color on a storyboard?

borderColor property. This can be accomplished by creating a Category on CALayer, setting the Key Path to a unique new "property" ( borderColorFromUIColor ), and in the category overriding the corresponding setter ( setBorderColorFromUIColor: ).


2 Answers

Actually you can set some properties of a view's layer through interface builder. I know that I can set a layer's borderWidth and cornerRadius through xcode. borderColor doesn't work, probably because the layer wants a CGColor instead of a UIColor.

You might have to use Strings instead of numbers, but it works!

enter image description here

But you can use categories to proxy properties such as layer.borderColor. (From the ConventionalC CocoaPod)

CALayer+XibConfiguration.h:

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

@interface CALayer(XibConfiguration)

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

@end

CALayer+XibConfiguration.m:

#import "CALayer+XibConfiguration.h"

@implementation CALayer(XibConfiguration)

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

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

@end

Interface Builder

The result will be apparent during runtime, not in Xcode.

like image 180
Baig Avatar answered Nov 03 '22 02:11

Baig


You can set most of those in the interface builder adding runtime attributes to the elements: enter image description here

For layer.borderWidth = 2.0f; would be:

Select the button and add a new attribute

keypath : layer.borderWidth

type: Number Value 2

These changes will not be visible inside the interface builder, only at runtime

like image 26
Istvan Avatar answered Nov 03 '22 02:11

Istvan