Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:52:51+00:00 2026-06-17T17:52:51+00:00

I have a BoardViewController (UIViewController) and need to draw centered coordinate lines into its

  • 0

I have a BoardViewController (UIViewController) and need to draw centered coordinate lines into its background. For these coordinate lines I created a custom UIView class CoordinateView which are added as subView. The coordinateView should be centered and fill the whole screen even when changing the device orientation.

To do this I’d like to use Auto Layout implemented in code. Here’s my current setup:

In the CoordinatesView (UIView) class a custom draw method for the coordinate lines

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 1.0);

    CGContextMoveToPoint(context, self.bounds.size.width/2,0);
    CGContextAddLineToPoint(context, self.bounds.size.width/2,self.bounds.size.height); 
    CGContextStrokePath(context);
    CGContextMoveToPoint(context, 0,self.bounds.size.height/2);
    CGContextAddLineToPoint(context, self.bounds.size.width,self.bounds.size.height/2); 

    CGContextStrokePath(context);
}

Initializing this coordinatesView object in the BoardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
...
        coordinatesView = [[CoordinatesView alloc]initWithFrame:self.view.frame];
        [coordinatesView setBackgroundColor:[UIColor redColor]];
        [coordinatesView clipsToBounds];
        [coordinatesView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.view addSubview:coordinatesView];
        [self.view sendSubviewToBack:coordinatesView];
...
}

Adding the auto layout magic to the coordinateView in the BoardViewController’s viewWillAppear function

-(void)viewWillAppear:(BOOL)animated{
...
NSLayoutConstraint *constraintCoordinatesCenterX =[NSLayoutConstraint
                                              constraintWithItem:self.view
                                              attribute:NSLayoutAttributeCenterX
                                              relatedBy:NSLayoutRelationEqual
                                              toItem:coordinatesView
                                              attribute:NSLayoutAttributeCenterX
                                              multiplier:1.0
                                              constant:1];

NSLayoutConstraint *constraintCoordinatesCenterY =[NSLayoutConstraint
                                                   constraintWithItem:self.view
                                                   attribute:NSLayoutAttributeCenterY
                                                   relatedBy:NSLayoutRelationEqual
                                                   toItem:coordinatesView
                                                   attribute:NSLayoutAttributeCenterY
                                                   multiplier:1.0
                                                   constant:1];

[self.view addConstraint: constraintCoordinatesCenterX];
[self.view addConstraint: constraintCoordinatesCenterY];
...
}

Note: This approach worked for me using an UIImageView Image as coordinates but it doesn’t work with the custom UIView coordinateView.

  • How do I make it work again? As soons as I apply the Auto Layout/NSLayoutConstraint my coordinatesView UIView seems disappears

  • Is this actually a good approach to add a background drawing to a UIViewController or is it better to directly draw into the UIViewController. (If so how would that look like?)

I appreciate your help with this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:52:53+00:00Added an answer on June 17, 2026 at 5:52 pm

    Your view disappears because you set no size constraints — you generally need 4 constraints to fully express the position and size of a view. Some views, like buttons have an intrinsic content size, so you don’t need to explicitly set the size. The same is true for image views, which get their size from the image they display.

    So, in your case, you can set the width and height equal to the width and height of the superview. This is something that I do often, so I’ve created a category on UIView that contains various constraint methods, so I don’t have to write this over and over. I have one method constrainViewEqual: that does what you want to do:

    #import "UIView+RDConstraintsAdditions.h"
    
    @implementation UIView (RDConstraintsAdditions)
    
    -(void)constrainViewEqual:(UIView *) view {
        [view setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
        NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
        NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
        NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
        NSArray *constraints = @[con1,con2,con3,con4];
        [self addConstraints:constraints];
    }
    

    Then, in your main code, you can call it like this:

    [self.view constrainViewsEqual:coordinatesView];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have a custom UITableViewCell which is part of a navigation controller. The cell
Have a simple iPhone app with a single UIViewController and two Views in one
Have a long running set of discrete tasks: parsing 10s of thousands of lines
Have a linking (or ref) table which has a dual primary key. Need to
Have created an android test project and currently trying to write android unit tests
Have just started to get into CakePHP since a couple of weeks back. I
Have a bunch of classes which I need to do serialize and deserialize from/to
Have have created a child of UITableViewCell , MenuItem which contains functionality for my
Have a TSQL view, which I need to group by one column, however, am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.