在两个UIScrollView之间添加垂直规则(边框)

时间:2011-01-13 23:02:05

标签: iphone ipad ios

我有两个UIScrollView实例,我想在它们之间绘制一条垂直线(不是100%高度,更像是80%)。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

使用核心图形。将两个UIScrollView嵌套在另一个视图中,并覆盖父级上的 drawRect 方法。有关详细信息,请参阅Apple的UIView documentation了解drawRect;有关绘图的详细信息,请参见Core Graphics Context Reference

- (void) drawRect: (CGRect) rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Draw divider
    UIGraphicsPushContext(context);
        // Fill the background, this must happen if the view is not opaque.
            if (self.opaque)
            {
            [[UIColor whiteColor ] set];
            CGContextFillRect(context, rect);
            }

        [[UIColor grayColor] set];

        CGContextSetLineWidth(context, 1);

        CGContextBeginPath(context);
        CGContextMoveToPoint(context, rect.size.width * 0.5, 0.1 * rect.size.height);
        CGContextAddLineToPoint(context, rect.size.width * 0.5, 0.9 * rect.size.height);
        CGContextStrokePath(context);
    UIGraphicsPopContext(); 
}

修改

为了解决我的疏忽问题,我不知道在子视图之上绘制的简单方法。

可以使用与上面相同的基本视图层次结构模拟此行为。子类UIView创建一个完全透明的视图,不接受任何触摸事件。在自定义视图中放置绘图代码(我在上面的代码中添加了一个不透明的条件),然后在视图层次结构的前面插入自定义视图的实例。

答案 1 :(得分:1)

您的视图之间的垂直线也可能只是一个薄而高的UIView(在滚动视图的顶部),例如,背景为纯色。这意味着您无需担心自定义绘图代码。这也允许您在Interface Builder中进行布局,使用自动调整等