创建一条水平线

问题描述 投票:37回答:6

我有两个堆叠的标签。如果我想在它们之间使用水平线,除了使用带有UIImageView的图像之外还有其他方法吗?

iphone cocoa-touch uiimageview uiimage
6个回答
84
投票

创建一个UIView,黑色背景为1像素高,320像素宽。


19
投票

使用UIView:

UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 1)];
separator.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
[self.view addSubview:separator];
[separator release];

12
投票

虽然Jasarien的解决方案很简单,但它不会在2X设备上创建实际的1像素发际线,而是2像素宽线。

我找到了关于如何创建真正的1像素细线的blog post。我们需要一个实用程序UIView子类。对于Swift来说:

import UIKit

class HairlineView: UIView {
    override func awakeFromNib() {
        guard let backgroundColor = self.backgroundColor?.CGColor else { return }
        self.layer.borderColor = backgroundColor
        self.layer.borderWidth = (1.0 / UIScreen.mainScreen().scale) / 2;
        self.backgroundColor = UIColor.clearColor()
    }
}

5
投票

对于水平线

UIView *horizontalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,1,linelenth)];
horizontalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:horizontalLine];
[horizontalLine release];

对于垂直线

UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,linelenth,1)];
verticalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:verticalLine];
[verticalLine release];

1
投票

试试这个

extension CALayer {

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {

        let border = CALayer()

        switch edge {
        case .top:
            border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
        case .bottom:
            border.frame = CGRect(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
        case .left:
            border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.height)
        case .right:
            border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
        default:
            break
        }

        border.backgroundColor = color.cgColor;

        addSublayer(border)
    }

0
投票

您可以将其放入UIView中

- (void)drawRect:(CGRect)rect
{

//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Shadow Declarations
CGColorRef outerShadow = [UIColor blackColor].CGColor;
CGSize outerShadowOffset = CGSizeMake(0, 1);
CGFloat outerShadowBlurRadius = 2;

//// Abstracted Graphic Attributes
CGRect rectangleFrame = CGRectMake(0, 0, self.frame.size.width, 3);


//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: rectangleFrame];
[[UIColor lightGrayColor] setFill];
[rectanglePath fill];

////// Rectangle Inner Shadow
CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -outerShadowBlurRadius, -outerShadowBlurRadius);
rectangleBorderRect = CGRectOffset(rectangleBorderRect, -outerShadowOffset.width, -outerShadowOffset.height);
rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1);

UIBezierPath* rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect];
[rectangleNegativePath appendPath: rectanglePath];
rectangleNegativePath.usesEvenOddFillRule = YES;

CGContextSaveGState(context);
{
    CGFloat xOffset = outerShadowOffset.width + round(rectangleBorderRect.size.width);
    CGFloat yOffset = outerShadowOffset.height;
    CGContextSetShadowWithColor(context,
                                CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
                                outerShadowBlurRadius,
                                outerShadow);

    [rectanglePath addClip];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0);
    [rectangleNegativePath applyTransform: transform];
    [[UIColor grayColor] setFill];
    [rectangleNegativePath fill];
}
CGContextRestoreGState(context);
}

0
投票

要从头开始在swift中创建一行,就这样做吧

let line = UIView()
view.addSubview(line)
line.translatesAutoresizingMaskIntoConstraints = false
line.widthAnchor.constraint(equalToConstant: view.bounds.width - 40).isActive = true
line.heightAnchor.constraint(equalToConstant: 1).isActive = true
line.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
line.topAnchor.constraint(equalTo: userName.bottomAnchor,constant: 20).isActive = true
line.backgroundColor = .gray

线的宽度等于器件宽度 - 一些常量,从左边添加相同的常量,所以我们得到这样的一行


© www.soinside.com 2019 - 2024. All rights reserved.