My app is placing a button with a custom background image and i doesn't need a border around it. I am not sure how to remove the border. My code is:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(100, 170, 100,30);
UIImage *cbutton = [UIImage imageNamed:@"pdficon_small.png"];
[button setImage:cbutton forState:UIControlStateNormal];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self
action:@selector(openWordings:)
forControlEvents:UIControlEventTouchDown];
[button setTag:2];
[self.view addSubview:button];
Thanks in advance.
I'm assuming you mean there is a border arround button
.
To remove it replace the line
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
with this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
What you are doing right now is creating a standard RoundedRect button, then placing an image over the top.
using UIButtonTypeCustom
will create what is essentially a "blank" button, allowing you to use your image.
I'm using a xib file that uses the default iOS7 buttons. I like them unless the user is on iOS6 -- then they get hit with the ugly iOS6 rounded buttons. This is a complete hack, but solved my problems. It just inserts 4 layers on each side of the button masking the appearance.
- (void)maskUglyIos6Border:(UIButton *)button
{
CALayer *layer = [CALayer layer];
// top layer
layer.frame = CGRectMake(0, 0, button.layer.frame.size.width, 5);
layer.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer];
// bottom layer
CALayer *layer2 = [CALayer layer];
layer2.frame = CGRectMake(0, button.layer.frame.size.height - 5, button.layer.frame.size.width, 5);
layer2.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer2];
// left layer
CALayer *layer3 = [CALayer layer];
layer3.frame = CGRectMake(0, 0, 5, button.layer.frame.size.height);
layer3.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer3];
// right layer
CALayer *layer4 = [CALayer layer];
layer4.frame = CGRectMake(button.layer.frame.size.width - 5, 0, 5, button.layer.frame.size.height);
layer4.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer4];
}
To keep with the iOS7 appearance, I would recommend disabling highlighting and instead using showsTouchWhenHighlighted
.
Since I use a plain white background this solved it for me. I inherit from UIButton so I can assign it easily using storyboard.
@implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// adjust buttons -> "remove" border if we don't run on ios 7
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
[self setBackgroundColor:[UIColor whiteColor]];
}
}
return self;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With