38

Is it possible to add a border just on top of a UIView, if so, how please?

GilbertOOl
  • 1,299
  • 2
  • 15
  • 27
  • you can set view.layer.bordercolor and borderwidth, bordercolor – rakeshNS Jul 12 '13 at 06:56
  • No sorry, your solution add a border for all view , not for ONLY top of UIView !!! – GilbertOOl Jul 12 '13 at 07:02
  • 1
    we can set border on one side of UIView, this post may be helpful http://stackoverflow.com/questions/11236607/uiview-set-only-side-borders – rakeshNS Jul 12 '13 at 07:05
  • possible duplicate of [UIView bottom border?](http://stackoverflow.com/questions/7666863/uiview-bottom-border) – Cœur Aug 03 '15 at 10:31
  • try this https://stackoverflow.com/questions/17355280/how-to-add-a-border-just-on-the-top-side-of-a-uiview/48293635#48293635 – Datt Patel Jan 17 '18 at 04:38

8 Answers8

98

I just Testing Bellow few line of Code and it works very nice, just test it in to your Project. hope you'll get your solution easily.

Why to create new View and adding it into your existing view..? For this task simply create one CALayer and add it into your existing UIView's Layer do as following:-

#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad
{
    CALayer *TopBorder = [CALayer layer];
    TopBorder.frame = CGRectMake(0.0f, 0.0f, myview.frame.size.width, 3.0f);
    TopBorder.backgroundColor = [UIColor redColor].CGColor;
    [myview.layer addSublayer:TopBorder];

  [super viewDidLoad];

}

and It's Output is:-

enter image description here

Akshay
  • 227
  • 2
  • 13
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
18

i've find solution for me, here's the tricks :

CGSize mainViewSize = self.view.bounds.size;
CGFloat borderWidth = 1;
UIColor *borderColor = [UIColor colorWithRed:37.0/255 green:38.0/255 blue:39.0/255 alpha:1.0];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, mainViewSize.width, borderWidth)];
topView.opaque = YES;
topView.backgroundColor = borderColor;
topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;        
[self.view addSubview:topView];
GilbertOOl
  • 1,299
  • 2
  • 15
  • 27
10

GilbertOOI's answer in Swift 2:

let topBorder: CALayer = CALayer()
topBorder.frame = CGRectMake(0.0, 0.0, myView.frame.size.width, 3.0)
topBorder.backgroundColor = UIColor.redColor().CGColor
myView.layer.addSublayer(topBorder)
brandonscript
  • 68,675
  • 32
  • 163
  • 220
6

Here's a UIView category that lets you add a layer-back or view-backed border on any side of the UIView: UIView+Borders

aroooo
  • 4,726
  • 8
  • 47
  • 81
5

GilbertOOI's answer in Swift 4:

let topBorder: CALayer = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: myView.frame.size.width, height: 1)
topBorder.backgroundColor = UIColor.purple.cgColor
myView.layer.addSublayer(topBorder)
dengApro
  • 3,848
  • 2
  • 27
  • 41
2

I created this simple UIView subclass so that it works in Interface Builder and works with constraints: https://github.com/natrosoft/NAUIViewWithBorders

Here's my blog post about it: http://natrosoft.com/?p=55

-- 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.borderColorTop = [UIColor redColor];
self.myBorderView..borderWidthsAll = 1.0f;

/* For borders with different colors and widths ————————— */
self.myBorderView.borderWidths = UIEdgeInsetsMake(2.0, 4.0, 6.0, 8.0);
self.myBorderView.borderColorTop = [UIColor blueColor];
self.myBorderView.borderColorRight = [UIColor redColor];
self.myBorderView.borderColorBottom = [UIColor greenColor];
self.myBorderView.borderColorLeft = [UIColor darkGrayColor];

Here's a direct link to the .m file so you can see the implementation: NAUIViewWithBorders.m
There is a demo project as well.

n8tr
  • 5,018
  • 2
  • 32
  • 33
2

remus' answer in Obj-C:

 CALayer *topBorder = [CALayer new];
 topBorder.frame = CGRectMake(0.0, 0.0, self.frame.size.width, 3.0);
 topBorder.backgroundColor = [UIColor redColor].CGColor;
 [myView.layer addSublayer:topBorder];
Andrew Bennett
  • 910
  • 11
  • 11
0

Swift5:

We will write a separate method to add borders to this view. To add borders to this view we will create two layers with the desired thickness. We will set the frame of these two layers to the top and bottom of the view. We will set the desired background color of the borders on these layers and add these layers as subLayers to the view.

func addTopBorders() {
   let thickness: CGFloat = 1.0
   let topBorder = CALayer()
   topBorder.frame = CGRect(x: 0.0, y: 0.0, width: 
   self.down_view_outlet.frame.size.width, height: thickness)
   topBorder.backgroundColor = UIColor.white.cgColor
   down_view_outlet.layer.addSublayer(topBorder)
}
Shahriar Hossain
  • 119
  • 2
  • 13