I'm trying to draw a horizontal line through the middle of the screen of an app that has a navigation bar. Here's my current code to do so:
CGFloat screenWidth = self.view.frame.size.width;
CGFloat screenHeight = self.view.frame.size.height;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat lineThickness = 3;
UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectMake(0, (screenHeight - navigationBarHeight) / 2, screenWidth, lineThickness)];
[self.view addSubview:horizontalLine];
I used a very similar calculation to draw a vertical line - and that worked just fine. But, the issue is, regardless of device, this horizontal line ends up too low on the screen by roughly 10 pixels. Is there an offset I may be missing? Something to do with the navigation bar perhaps?
ok i did it like this first .. but it wasnt centered. but it SHOULD BE! :) under this i changed it to add a view to wrap it all and that seemed to "work" you can try either or, or a combination of some of the elements and see how that goes.
UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:horizontalLine];
[horizontalLine setTranslatesAutoresizingMaskIntoConstraints: NO];
horizontalLine.backgroundColor = [UIColor blackColor];
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(horizontalLine, topGuide, bottomGuide);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[horizontalLine]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topGuide][horizontalLine(==3)][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:horizontalLine
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
"wrapped" approach. it's a bit messy but it might give you some ideas of things to try
UIView * wrapper = [[UIView alloc] initWithFrame:CGRectZero];
UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectZero];
[wrapper setTranslatesAutoresizingMaskIntoConstraints: NO];
[horizontalLine setTranslatesAutoresizingMaskIntoConstraints: NO];
[self.view addSubview:wrapper];
[wrapper addSubview:horizontalLine];
horizontalLine.backgroundColor = [UIColor blackColor];
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(horizontalLine, topGuide, bottomGuide, wrapper);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[wrapper]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topGuide][wrapper][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary]];
[wrapper addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[horizontalLine]|" options:0 metrics: 0 views:viewsDictionary]];
[wrapper addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=1)-[horizontalLine(==3)]-(>=1)-|" options:0 metrics: 0 views:viewsDictionary]];
[wrapper addConstraint:
[NSLayoutConstraint constraintWithItem:horizontalLine
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:wrapper
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];