Swift add line above to control

妖精的绣舞 提交于 2019-12-07 07:44:32

问题


In the below screen, I want to add horizontal line above to "Item" label and after "Add" button (below to the add button, I have dynamic table). UIGraphicsGetCurrentContext() doesn't work here.

Can someone help me how to do this?


回答1:


It is simple to add a subview to act as a line. For example:

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];

Or you can refer to this link to add CALayer or draw a view

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




回答2:


You can achieve this in Storyboard.

All you have to do is drag a UIView with height = 1 and width whatever is good for you (ideally equal to screen width). Place it where you want the lines to be.




回答3:


For 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)


来源:https://stackoverflow.com/questions/30090939/swift-add-line-above-to-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!