我已在viewdidload()中应用了以下代码,但是使用此代码边框已到达,但它使用表内容滚动。我希望在顶部和底部修复边框。
这里我的代码是 - >
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)
给我建议和谢谢
答案 0 :(得分:4)
我使用以下方法为任何视图添加边框。请看看它是否对您有所帮助。
//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 :(得分:-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;
只需使用适当的值
提供此方法