2017-04-21 117 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; 

只給這個方法用適當的值

相關問題