繁体   English   中英

ios:我想在tableview的顶部和底部添加边框

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

我已经在viewdidload()中应用了下面的代码,但是使用了此代码边框,但是它与表content一起滚动。我想在顶部和底部固定边框。

这是我的代码->

    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)

给我建议和感谢

我正在使用以下方法将边框添加到任何视图。 请查看它是否对您有帮助。

//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;

只需将此方法输入适当的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM