Draw line in UIView

后端 未结 8 849
粉色の甜心
粉色の甜心 2020-12-02 05:52

I need to draw a horizontal line in a UIView. What is the easiest way to do it. For example, I want to draw a black horizontal line at y-coord=200.

I am NOT using In

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 06:31

    The easiest way in your case (horizontal line) is to add a subview with black background color and frame [0, 200, 320, 1].

    Code sample (I hope there are no errors - I wrote it without Xcode):

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lineView];
    [lineView release];
    // You might also keep a reference to this view 
    // if you are about to change its coordinates.
    // Just create a member and a property for this...
    

    Another way is to create a class that will draw a line in its drawRect method (you can see my code sample for this here).

提交回复
热议问题