菜鸟教程小白 发表于 2022-12-12 09:22:42

ios - 在 UIView 上绘制顶线和底线


                                            <p><p>我正在尝试在 <code>UIView</code> 的顶部和底部边缘绘制线条。但是这条线不会一直画到 ViewController 的右边缘。</p>

<p>以下是我用来绘制线条的代码:</p>

<pre><code>- (void)addBorders
{
    CALayer *upperBorder = ;
    CALayer *bottomBorder = ;
    upperBorder.backgroundColor = [ CGColor];
    upperBorder.frame = CGRectMake(0, 0, CGRectGetWidth(self.recentTuneinView.frame), 0.5f);
    bottomBorder.backgroundColor = [ CGColor];
    bottomBorder.frame = CGRectMake(0, 58.0f, CGRectGetWidth(self.recentTuneinView.frame), 0.5f);
    ;
    ;

}
</code></pre>

<p>这是显示问题的图像:</p>

<p> <a href="/image/f31aR.png" rel="noreferrer noopener nofollow"><img src="/image/f31aR.png" alt="enter image description here"/></a> </p>

<p>我在代码中遗漏了什么?</p>

<p>谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>添加子层不是一个可扩展的解决方案,因为它会在旋转设备或更改 View 大小时产生问题。</p>

<p>我的建议是创建一个自定义 View 并实现 <code>drawRect:</code> 类似这样的东西:</p>

<pre><code>- (void)drawRect:(CGRect)iRect {
    CGContextRef aContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(aContext, 0.5);

    // Set Top Line Color
    CGContextSetStrokeColorWithColor(aContext, [ CGColor]);

    // Top Line
    CGContextMoveToPoint(aContext, 0, 0);
    CGContextAddLineToPoint(aContext, iRect.size.width, 0);

    // Set Bottom Line Color
    CGContextSetStrokeColorWithColor(aContext, [ CGColor]);

    // Bottom Line
    CGContextMoveToPoint(aContext, 0, 58.0);
    CGContextAddLineToPoint(aContext, iRect.size.width, 58.0);
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 UIView 上绘制顶线和底线,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33172607/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33172607/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 UIView 上绘制顶线和底线