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

前端 未结 8 1522
天命终不由人
天命终不由人 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)
    }
    

提交回复
热议问题