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

  • Home
  • SEARCH
  • 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 3277510
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:22:37+00:00 2026-05-17T19:22:37+00:00

Starting off with a default View based application I create a new view (ViewToDisplay,

  • 0

Starting off with a default “View based” application I create a new view (ViewToDisplay, class that inherits from UIView) and inside this view I create a layer (LayerToDisplay). The view draws something around the perimeter of the frame, and so does the layer but this time with dotted lines. This is to show / prove that the layer covers the entire view.

Here’s the relevant code from ViewToDisplay.m

- (void) awakeFromNib
{
    ld = [[LayerToDisplay alloc] init];
    ld.frame = self.frame;
    [self.layer addSublayer:ld];
    [ld setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSetRGBStrokeColor(context, 255/255.0, 0/255.0, 0/255.0, 1);
    CGContextSetLineWidth(context, 1);

    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 320, 460);
    CGContextAddLineToPoint(context, 0, 460);
    CGContextAddLineToPoint(context, 320, 0);

    CGContextClosePath(context);
    CGContextStrokePath(context);
}

and the layer (LayerToDisplay.m)

- (void)drawInContext:(CGContextRef)context
{
    CGContextBeginPath(context);

    CGContextSetRGBStrokeColor(context, 0/255.0, 255/255.0, 0/255.0, 1);
    CGContextSetLineWidth(context, 1);

    CGFloat dashes[]={3,6};
    CGContextSetLineDash(context, 0, dashes, 3);

    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 320, 460);
    CGContextAddLineToPoint(context, 0, 460);
    CGContextAddLineToPoint(context, 320, 0);

    CGContextClosePath(context);
    CGContextStrokePath(context);
}

If I change the default ApplicationNameViewController.xib’s view to my class (ViewToDisplay) it works as expected. If I try to instantiate this from the application delegate the view shows up correctly but the layer is shifted down, i.e. what the layer draws doesn’t overlap what the view draws.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    ViewToDisplay *vd = [[ViewToDisplay alloc] init];
    // doesn't seem to matter
    //[vd setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    vd.frame = [UIScreen mainScreen].applicationFrame;
    // call manually so that the layer gets created
    [vd awakeFromNib]; 
    [window addSubview:vd]; 

    //[window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

So, I’m trying to figure out what’s the difference between creating / instantiating a view with these two ways. What does the automatic process (when changing an XIB’s class to ViewToDisplay) do that I’m not doing in code?

Thank you!

P.S. this is an exercise in understanding how it works behind the scenes, not in using best practices.

  • 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-05-17T19:22:38+00:00Added an answer on May 17, 2026 at 7:22 pm

    In awakeFromNib use ld.frame = self.bounds; instead of ld.frame = self.frame;.

    To explain what’s going on – if the view is loaded by view-controller, that view has its frame starting at point (0, 0). If you are using UIScreen’s applicationFrame, that is positioned down by size of the status bar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.