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 6836297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:20+00:00 2026-05-26T23:20:20+00:00

So I have a class that returns a CALayer that has an array of

  • 0

So I have a class that returns a CALayer that has an array of CALayers added as sublayers, but the problem is that it doesn’t seem to be rendering any of them. I think I may just be missing something about how CALayers work as I’m not very familiar yet. Could someone please take a look at my code to see what I am doing wrong?

- (CALayer*)drawMap {
    CALayer* theLayer = [CALayer layer];
    for (int y = 0; y < [self.height intValue]; y++) {
        for (int x = 0; x < [self.width intValue]; x++) {
            CALayer* tileLayer = [CALayer layer];
            tileLayer.bounds = CGRectMake((x * [self.tileWidth intValue]), (y * [self.tileHeight intValue]), [self.tileWidth intValue], [self.tileHeight intValue]);
            [tileLayer setBackgroundColor:[UIColor colorWithHue:(CGFloat)x saturation:(CGFloat)y brightness:255 alpha:1.0].CGColor];
            [theLayer addSublayer:tileLayer];
        }
    }
    [theLayer setBounds:CGRectMake([self.width intValue] * [self.tileWidth intValue], [self.height intValue] * [self.tileHeight intValue], 10, 10)];
    return theLayer;       
}

This is the code that initializes and draws the map.

Map* myMap = [[Map alloc] initFromFile];
[myMap setWidth:[NSNumber numberWithInt:100]];
[myMap setHeight:[NSNumber numberWithInt:100]]; 

[self.view.layer insertSublayer:[myMap drawMap] above:self.view.layer];

I can provide the headers if needed, but I don’t think the problem is there and I don’t want a huge post.

  • 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-26T23:20:21+00:00Added an answer on May 26, 2026 at 11:20 pm

    I think the problem is the bounds you’re setting to your parent layer:

    [theLayer setBounds:
                    CGRectMake(
                               [self.width intValue] * [self.tileWidth intValue], 
                               [self.height intValue] * [self.tileHeight intValue], 
                               10, 10)];
    

    That would appear to set the bounds so that it starts just exactly past all the sublayers you’ve just added and extends for 10 points in both directions. I think the following would be more appropriate:

    [theLayer setBounds:
                    CGRectMake(
                               0, 
                               0, 
                               [self.width intValue] * [self.tileWidth intValue], 
                               [self.height intValue] * [self.tileHeight intValue])];
    

    The following also looks fishy:

    [self.view.layer insertSublayer:[myMap drawMap] above:self.view.layer];
    

    [myMap drawMap] will become a sublayer of self.view.layer. self.view.layer is not one of its own subviews so it won’t know what to do with that above: instruction. You probably want just addSublayer:.

    Finally, this:

    tileLayer.bounds = CGRectMake((x * [self.tileWidth intValue]), (y * [self.tileHeight intValue]), [self.tileWidth intValue], [self.tileHeight intValue]);
    

    gives every single one of your sublayers exactly the same frame. Much like UIViews, the bounds are the source area for information and the frame is where that information will be put within the super layer. I think probably you want to set frame.

    Finally, UIColor +colorWithHue:... clamps hue and saturation to the range [0, 1] so you probably want (CGFloat)x / [self.width floatValue] and the equivalent for saturation to ensure all your tiles come out in different colours.

    EDIT: so, tying all that together, I modified your code slightly so that I didn’t have to write the full encompassing class and added it to the Xcode view-based project template as follows:

    #define kTileWidth  3
    #define kTileHeight 3
    #define kWidth 100
    #define kHeight 100
    
    - (CALayer*)drawMap {
        CALayer* theLayer = [CALayer layer];
        for (int y = 0; y < kHeight; y++)
        {
            for (int x = 0; x < kWidth; x++)
            {
                CALayer* tileLayer = [CALayer layer];
                tileLayer.frame = CGRectMake((x * kTileWidth), (y * kTileHeight), kTileWidth, kTileHeight);
                [tileLayer setBackgroundColor:[UIColor colorWithHue:(CGFloat)x/kWidth saturation:(CGFloat)y/kHeight brightness:255 alpha:1.0].CGColor];
                [theLayer addSublayer:tileLayer];
            }
        }
        [theLayer setFrame:CGRectMake(0, 0, kWidth * kTileWidth, kHeight * kTileHeight)];
        return theLayer;       
    }
    
    // ...
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        [self.view.layer addSublayer:[self drawMap]];
    }
    

    That gives the hue/saturation plane familiar from many an art package.

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

Sidebar

Related Questions

I have a class that has an output() method which returns a matplotlib Figure
I have a class in VB.NET that has a method (called CurrentValue) that returns
I have class method that returns a list of employees that I can iterate
I have a function inside a class that returns a reference to a member
I have a utility class that creates & returns a db connection: Public Shared
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
I have an ASP.NET WebService that returns an object of List public class Students
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a static method that takes a parameter and returns a class. the
I have a class that returns an object type to a variable. The variable

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.