Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios5:delete UIButton border

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.

like image 569
user973067 Avatar asked Jan 30 '12 17:01

user973067


3 Answers

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.

like image 134
Amit Shah Avatar answered Nov 20 '22 03:11

Amit Shah


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.

like image 38
joslinm Avatar answered Nov 20 '22 05:11

joslinm


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;

}

like image 42
HeyMan Avatar answered Nov 20 '22 03:11

HeyMan