2017-04-21 80 views
-1

我已经在viewdidload()中应用了下面的代码,但是使用这个代码边框到达了,但是它随着表格内容滚动。我想要修复顶部和底部的边框侧。ios:我想在tableview顶部和底部添加边框

这里我的代码 - >

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) 

给我suggetion和感谢

+0

添加的层是它是不是从你的代码清晰。 CGPoints的数学算法也是不正确的,因为图层将被添加到边界外。 – Oskar

回答

0

我使用下面的方法来添加边框任何观点。请看看它是否对你有帮助。

//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) 
} 
-1

我有一个使用少量枚举来实现你想要做的事情的方法。请参阅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; 

只给这个方法用适当的值

相关问题