如何在UIScrollView中绘制垂直线

xon*_*rlz 0 iphone objective-c ipad ios

我有一个UIScrollView,我想绘制一个具有UIScrollView contentHeight高度的垂直线.如何在不牺牲性能的情况下轻松完成此操作?我正在考虑使用具有非常大的高度的UIVIew(因为我们不知道scrollView的contentHeight将是什么)并将其添加为UIScrollView的子视图.有比这更好的方法吗?

该线基本上是10px宽度,灰色,从scrollView的顶部到底部跨越.

这是我现在所拥有的,我基本上覆盖了UIScrollView drawRect:

- (void)drawRect:(CGRect)rect
{
    if (shouldDrawVerticalLineForProfile){
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGColorRef separatorColor = [UIColor colorWithRed:47.0/255.0 green:47.0/255.0 
                                                     blue:47.0/255.0 alpha:1.0].CGColor;

        // Add at bottom
        CGPoint startPoint = CGPointMake(30, 0);
        CGPoint endPoint = CGPointMake(30, 10000);

        CGContextSaveGState(context);
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextSetStrokeColorWithColor(context, separatorColor);
        CGContextSetLineWidth(context, 5.0);
        CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
        CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
        CGContextStrokePath(context);
        CGContextRestoreGState(context);     
    }

}
Run Code Online (Sandbox Code Playgroud)

Col*_*gic 5

嗯,最简单的方法就是添加一个UIView并设置背景颜色.

-(void)viewDidLoad {
   [super viewDidLoad];

   UIView *verticalBar = [[[UIView alloc] init] autorelease];
   [verticalBar setBackgroundColor:[UIColor grayColor]];
   [verticalBar setFrame:CGRectMake(0,0,10,[scrollView contentSize].height)];

   [[self view] addSubview:verticalBar];
}
Run Code Online (Sandbox Code Playgroud)

您不需要"非常大的高度",因为您通过scrollView知道最大高度.