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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:46:27+00:00 2026-06-18T02:46:27+00:00

I am working with a grouped table, and am customizing the header in the

  • 0

I am working with a grouped table, and am customizing the header in the sections using tableView: viewForHeaderInSection: method and setting the height using tableView: heightForHeaderInSection:.

I created a view and placed a label in it like so:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {


    // Create a custom title view.
    UIView *ctv;
    UILabel *titleLabel;

    // Set the name.
    {...} // Code not relevant

    // If an iPhone.
    if ([Config isPhone]) {
        ctv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 36)];
    }
    // If an iPad
    else {
        ctv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 75)];
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 544, 55)];
    }

    // Config the label.
    {...} // Code not relevant

    // Center the items.
    ctv.center = CGPointMake(tableView.center.x, ctv.center.y);
    titleLabel.center = ctv.center;

    // Add the label to the container view.
    [ctv addSubview:titleLabel];

    // Return the custom title view.
    return ctv;


}

This all works great until you rotate the screen. The position is off. I realize that this is because the view is being centered while it is in the other orientation causing the calculation of the center to no longer be correct. The solution should be to add a constraint. I tried adding the constraint below:

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(ctv);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[ctv]|"
                         options:0
                         metrics:nil
                         views:viewsDictionary
                       ];
[tableView addConstraints:constraints];

But when I do this trying the method below, I get that no parent view is associated with it, which makes complete sense, because it doesn’t technically get added into the view is returned. So I thought I would try to add the constraint this way:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:ctv
                                     attribute:NSLayoutAttributeCenterX
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:tableView
                                     attribute:NSLayoutAttributeCenterX
                                    multiplier:1.0
                                      constant:0
                                      ];
[tableView addConstraint:constraint];

But this one also errors. I’ve tried switching the tableView variables to the global table property but it gives the same results. I also tried to figure out how to add the constraint in the view did load method but it failed as I could not figure out how to get back to the table’s section headers from the table object. The last thought I had was to set the width on the table in a constraint and set one to center the entire table. This process worked but now I have the an ugly scroll in the middle of my app when it is in the landscape orientation. So the question is, where/how can I access the individual section headers after they have been loaded to add this constraint? I’m still pretty new to Objective-C so any help is appreciated.

***** NEW CODE BASED FROM rdelmar SUGGESTION ****

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *ctv = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"groupHeader"];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 544, 55)];
    [titleLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:ctv
                                        attribute:NSLayoutAttributeCenterX
                                        relatedBy:0
                                          toItem:titleLabel
                                        attribute:NSLayoutAttributeCenterX
                                       multiplier:1.0
                                         constant:0
                                      ];
    [ctv addConstraints:@[constraint]];
    titleLabel.text = @"string";
    [ctv addSubview:titleLabel];
    return ctv;
}

But like I mentioned, it is giving me a “Constraint must contain a first layout item” error.

  • 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-18T02:46:28+00:00Added an answer on June 18, 2026 at 2:46 am

    If you can’t get the constraints to work, your original code supplemented by an autoresizing mask (flexible left and right margins) would do the job.

    An even simpler solution would be to return a UILabel as the header view, with centered text.

    Your first attempt at constraints wouldn’t work because you are setting them up wrong.

    The table view is responsible for setting the frame of the header view. You need to worry about the position of the label within the header. The VFL for this would be "|titleLabel|" – the title label should be sized to its superview, the header view.

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

Sidebar

Related Questions

I'm working on creating a grouped table view. The data is being loaded alright,
I working on grouped table view based work in that in that i want
Hello I have a trouble customizing a background of the grouped table view.. Here's
I have created a grouped table view & I had a edit button in
I have a grouped table view with 2 sections. Section number 1 is hidden,
I have a table view which is grouped according to month and year. For
From coded detailView of my app which has a grouped table view. I want
I am working on displaying rows which are grouped on WPF DataGrid Group Header
I have build a grouped tableview with 2 sections. There always have to be
I am working on Entity frame work, i have created a method which is

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.