Swift 在上面添加一行来控制

Swift add line above to control

在下面的屏幕中,我想在 "Item" 标签上方和 "Add"[=21= 之后添加水平线] 按钮(在添加按钮下方,我有动态 table)。 UIGraphicsGetCurrentContext() 在这里不起作用。

有人可以帮我怎么做吗?

添加子视图充当一行很简单。例如:

Swift 4

var lineView = UIView(frame: CGRect(x: 0, y: 100, width: 320, height: 1.0))
lineView.layer.borderWidth = 1.0
lineView.layer.borderColor = UIColor.black.cgColor
self.view.addSubview(lineView)

Objective C

UIView * lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 100,320,1)];
lineview.layer.borderColor = [UIColor blackColor].CGColor;
lineview.layer.borderWidth = 1.0;
[self.view addSubview:lineview];

或者你可以参考这个link添加CALayer或者绘制视图

how do you draw a line programmatically from a view controller?

您可以在 故事板 中实现。

您所要做的就是拖动一个 UIViewheight = 1 以及 width 任何适合您的东西(理想情况下等于屏幕宽度)。把它放在你想要的地方。

对于Swift 3:

        let lineView = UIView(frame: CGRect(x:0,
                                            y:  self.yourLabel.height - 1 ,
                                            width: self.yourLabel.frame.width,
                                            height: 1.4
                                           )
                             )

        lineView.backgroundColor = UIColor.blue
        self.yourLabel.addSubview(lineView)