Iphone 创建水平线
我有两个堆叠的标签。如果我想在它们之间画一条水平线,除了使用带有UIImageView的图像,还有其他方法吗?创建一个黑色背景的UIView,背景高1像素,宽320像素。使用UIView:Iphone 创建水平线,iphone,cocoa-touch,uiimageview,uiimage,Iphone,Cocoa Touch,Uiimageview,Uiimage,我有两个堆叠的标签。如果我想在它们之间画一条水平线,除了使用带有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
UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 1)];
separator.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
[self.view addSubview:separator];
[separator release];
您可以将其放到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);
}
水平线
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];
虽然Jasarien的解决方案很好且简单,但它并没有创建一条真正的1像素发际线,而是在2X设备上创建一条2像素宽的发际线 我找到了一个关于如何创建真正的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()
}
}
试试这个
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)
}
要从头开始在swift中创建一行,只需执行以下操作
let line=UIView()
view.addSubview(行)
line.translates自动调整大小gmaskintoConstraints=false
line.widthAnchor.constraint(equalToConstant:view.bounds.width-40).isActive=true
line.heightAnchor.constraint(equalToConstant:1).isActive=true
line.leftAnchor.constraint(equalTo:view.leftAnchor,常量:20)。isActive=true
line.topAnchor.constraint(equalTo:userName.bottomAnchor,常量:20)。isActive=true
line.backgroundColor=.gray
线的宽度等于设备的宽度-一些常数,相同的常数从左边加起来,我们得到一条类似这样的线
克鲁德,我花了一分钟时间。这个回答让人觉得不屑一顾。也许可以解释一下如何在Interface Builder中实现这一点?创建2像素宽的线条,请参阅@Phantrast的答案以了解更薄的线条line@KevM什么部分需要解释?这是一个有10年历史的问题和答案。让它去吧事实上我认为这应该是正确的答案!非常好的解决方案Thx很多!