Add just a top border to an UIView with Quartzcore/layer?

前端 未结 8 1493
天命终不由人
天命终不由人 2020-12-13 17:58

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

相关标签:
8条回答
  • 2020-12-13 18:46

    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)
    }
    
    0 讨论(0)
  • 2020-12-13 18:53

    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];
    
    0 讨论(0)
提交回复
热议问题