2017-04-21 28 views
-1

私はviewdidload()で以下のコードを適用しましたが、このコードボーダーを使用していますが、table content.iでスクロールすると、側。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

適切な値でこのメソッドを養う

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

上記の方法のためにこれらの列挙型を使用してご覧ください。 。あなたの '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

私は、あなたがしたいことを達成するために少数の列挙型を使用する方法があります。また

-(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; 
} 

以下objective cコードはジャスト層が追加されている場合それはあなたのコードからは明らかではない

関連する問題