Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add only a TOP border on a UIButton?

I know how to add border to a button in iOS 7, with the following code :

[[myButton layer] setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; [[myButton layer] setBorderWidth:1]; [[myButton layer] setCornerRadius:15]; 

But how can I add just one border ? I want to add only the top border.

like image 731
Jonathan F. Avatar asked Nov 10 '13 14:11

Jonathan F.


People also ask

How do you add a border to the top side of UIView?

-- Basically just drop in a UIView in Interface Builder and change its class type to NAUIViewWithBorders. -- Then in your VC's viewDidLoad do something like: /* For a top border only ———————————————- */ self. myBorderView.

How do I turn off highlighting in Uibutton?

make your button Type - "Custom" and Uncheck - Highlighted Adjust image and you are done.

What is Uibutton Swift?

A control that executes your custom code in response to user interactions.


1 Answers

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, 1)]; lineView.backgroundColor = [UIColor redColor]; [btn addSubview:lineView]; 

you can do the same for each border. Adding multiple UIViews you can add bottom and left or top and right or any border you want.

i.e. bottom & left:

UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, btn.frame.size.height - 1.0f, btn.frame.size.width, 1)]; bottomBorder.backgroundColor = [UIColor redColor];  UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(1, 0, 1, btn.frame.size.height)]; leftBorder.backgroundColor = [UIColor redColor];  [btn addSubview:bottomBorder]; [btn addSubview:leftBorder]; 

if you don't use ARC, remember to release UIViews after adding subviews (or use autorelease).

like image 195
masgar Avatar answered Sep 20 '22 19:09

masgar