创建水平线

我有两个正在堆叠的标签。 如果我在它们之间需要水平线,则还有另一种方法来使用映像 UIImageView?
已邀请:

裸奔

赞同来自:

创建 UIView 黑色背景高度 1 像素和宽度 320 像素。

涵秋

赞同来自:

使用 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];

八刀丁二

赞同来自:

决定 Jasarien 很简单,它没有创造

实际的头发生长线 1 像素

, 和线宽宽度 2 设备上的像素 2X.

我发现了一条消息
http://www.iriphon.com/2014/02 ... -ios/
如何创建一个厚实的薄发线 1 像素。
我们需要一个子类实用程序 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//
}
}

石油百科

赞同来自:

对于水平线


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];

君笑尘

赞同来自:

试试吧


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/
}

奔跑吧少年

赞同来自:

你可以退出它 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/;
}

董宝中

赞同来自:

创建一个字符串 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


线宽等于设备的宽度 - 一些常量,左侧添加相同的常量,所以我们得到一个关于所以的一线

要回复问题请先登录注册