ios: 我想在表格视图的顶部和底部添加边框

ios: i want to add border at top and bottom side in tableview

i have applied below code in viewdidload() but using this code border is arrive but it scrolling with table content.i want fix border at top and bottom side.

here my code is ->

    let topBorder = CAShapeLayer()
    let topPath = UIBezierPath()
    topPath.move(to: CGPoint(x: 0, y: 0))
    topPath.addLine(to: CGPoint(x: tblFilter.frame.width, y: 0))
    topBorder.path = topPath.cgPath
    topBorder.strokeColor = UIColor.red.cgColor
    topBorder.lineWidth = 1.0
    topBorder.fillColor = UIColor.red.cgColor
    tblFilter.layer.addSublayer(topBorder)

    let bottomBorder = CAShapeLayer()
    let bottomPath = UIBezierPath()
    bottomPath.move(to: CGPoint(x: 0, y: tblFilter.frame.height))
    bottomPath.addLine(to: CGPoint(x: tblFilter.frame.width, y: tblFilter.frame.height))
    bottomBorder.path = bottomPath.cgPath
    bottomBorder.strokeColor = UIColor.red.cgColor
    bottomBorder.lineWidth = 1.0
    bottomBorder.fillColor = UIColor.red.cgColor
    tblFilter.layer.addSublayer(bottomBorder)

give me suggetion and thanks

我正在使用以下方法为任何视图添加边框。如果对你有帮助,请看看。

//MARK: - Add Border to View -
func addTopBorderWithColor(_ objView : UIView, color: UIColor, width: CGFloat) {
let border = CALayer()
border.backgroundColor = color.cgColor
border.frame = CGRect(x: 0, y: 0, width: objView.frame.size.width, height: width)
objView.layer.addSublayer(border)
}

func addBottomBorderWithColor(_ objView : UIView, color: UIColor, width: CGFloat) {
let border = CALayer()
border.backgroundColor = color.cgColor
border.frame = CGRect(x: 0, y: objView.frame.size.height - width, width: objView.frame.size.width, height: width)
objView.layer.addSublayer(border)
}

我有一个方法,它使用很少的枚举来实现你想做的事情。请参阅下面的 objective c 代码

-(void) addUIViewAt:(postionOfView) position OfView:(UIView *) view ofHeight:(int) height ofBackgroundColor:(UIColor *)backgroundColor{
UIView *viewForBorder = [[UIView alloc] init] ;
if (position == positionTop){
    [viewForBorder setFrame:CGRectMake(view.bounds.origin.x
                                       , view.bounds.origin.y, view.bounds.size.width, height)];
}
else if (position == positionBottom){
    [viewForBorder setFrame:CGRectMake(view.bounds.origin.x
                                       , view.bounds.size.height - height, view.bounds.size.width, height)];
}
else if (position == positionLeft){
    [viewForBorder setFrame:CGRectMake(view.bounds.origin.x
                                       , view.bounds.origin.y, height, view.bounds.size.height)];
}
else{
    [viewForBorder setFrame:CGRectMake(view.bounds.size.width - height
                                       , view.bounds.origin.y, height, view.bounds.size.height)];
}
[viewForBorder setBackgroundColor:backgroundColor];
[view addSubview:viewForBorder];
//    viewForBorder.layer.zPosition = MAXFLOAT;
}

上述方法也使用这些枚举

/**
this used to detect the position of border overlay on a view
*/
typedef enum : NSUInteger {
positionTop,
positionRight,
positionLeft,
positionBottom,
} postionOfView;

只需为该方法提供适当的值