【问题标题】:ios: i want to add border at top and bottom side in tableviewios:我想在tableview的顶部和底部添加边框
【发布时间】:2017-04-21 09:37:58
【问题描述】:

我已经在 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)

给我建议,谢谢

【问题讨论】:

  • 从您的代码中不清楚添加图层的位置。 CGPoints 的数学运算也不正确,因为图层将被添加到边界之外。

标签: ios swift uitableview layer


【解决方案1】:

我正在使用以下方法为任何视图添加边框。看看对你有没有帮助。

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

【讨论】:

    【解决方案2】:

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

    只需为这个方法提供适当的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2014-06-06
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多