I have two labels that are stacked. If I want a horizontal line between them, is there any other way besides using an image with a UIImageView?
HTML <hr> Tag. The <hr> tag in HTML stands for horizontal rule and is used to insert a horizontal rule or a thematic break in an HTML page to divide or separate document sections. The <hr> tag is an empty tag, and it does not require an end tag.
Create a UIView with a black background that is 1 pixel high and 320 pixels wide.
Use a 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];
While Jasarien's solution is nice and simple, it doesn't create an actual 1 pixel hairline, but a 2 pixel wide line on 2X devices.
I found a blog post on how to create the real 1 pixel thin hairline. We need a utility UIView subclass. For Swift that would be:
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With