Tworzenie poziomej linii


Mam dwie etykiety, które są ułożone w stos. Jeśli potrzebuję poziomej linii między nimi, czy istnieje inny sposób niż użycie obrazu z UIImageView?
Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Utwórz UIView z czarnym tłem o wysokości 1 piksela i szerokości 320 pikseli.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Użyj 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];
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Chociaż rozwiązanie Jasarien jest ładne i proste, nie tworzy

rzeczywista linia włosów o wielkości 1 piksela

i linia o szerokości 2 pikseli na urządzeniach 2x.
Znalazłem post na

blog
http://www.iriphon.com/2014/02 ... -ios/
Jak stworzyć naprawdę cienką linię włosów 1px.
Potrzebujemy podklasy narzędzia UIView. Dla Swifta byłoby to:
import UIKitclass 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()
}
}
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

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

Dla linii pionowej
UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,linelenth,1)];
verticalLine.backgroundColor = [UIColor blackColor];
[self. view addSubView:verticalLine];
[verticalLine release];
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Spróbuj
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)
}
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Możesz przesłać to do 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);
}
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Aby szybko utworzyć ciąg od zera, po prostu zrób to
<pre class="lang-swift prettyprint-override">
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

Szerokość linii jest równa szerokości urządzenia - pewna stała, a ta sama stała jest dodawana po lewej stronie, więc otrzymujemy taką linię

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się