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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:16:33+00:00 2026-06-05T19:16:33+00:00

I created a Category on UIView with a method who’s purpose it to create

  • 0

I created a Category on UIView with a method who’s purpose it to create and return a UIView object. It runs without errors but returns an empty UIView. Here’s the code:

#import <UIKit/UIKit.h>

@interface UIView (makeTableHeader)


 -(UIView *) makeTableHeader:(NSString *)ImageName
                  withTitle:(NSString *)headerTitle
                  usingFont:(NSString *)fontName 
                andFontSize:(CGFloat)fontSize;


@end

Here’s the Implementation:

-(UIView *) makeTableHeader: (NSString *)ImageName 
              withTitle:(NSString *)headerTitle 
              usingFont:(NSString *)fontName 
            andFontSize:(CGFloat)fontSize {

     // Create a master-view:
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];

     // Create the Image:
     UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ImageName]];
     headerImageView.frame = CGRectMake(0, 0, 320, 34);


     // Now create the Header LABEL:
     UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 34)];
     headerLabel.text = headerTitle;
     headerLabel.font = [UIFont fontWithName:fontName size:fontSize];
     headerLabel.backgroundColor = [UIColor clearColor];
     headerLabel.textColor = [UIColor whiteColor];
     headerLabel.shadowColor = [UIColor blackColor];
     headerLabel.shadowOffset = CGSizeMake(1.0, 1.0);

     // Finally add both both Header and Label as subview to the main Header-view:
     [headerView addSubview:headerImageView];
     [headerView addSubview:headerLabel];

     return headerView;
}

Now here’s how I call this category-method:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *hView = [[UIView alloc] init];
    [hView makeTableHeader:@"redGradientHeader5@2x.jpg"
                 withTitle:@"Test Banner"
                 usingFont:@"boldSystemFont"
               andFontSize:18];

    return hView;

}

The code runs fine – but I get an empty view. Interestingly it does size the view correctly – giving it the CGRect coordinates I asked for – but there’s no image or label inside the view.

Anyone see what’s wrong?

  • 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-05T19:16:35+00:00Added an answer on June 5, 2026 at 7:16 pm

    You need to make the method a class method, and assign it like this:

    UIView *hView = [UIView makeTableHeader:@"redGradientHeader5@2x.jpg"
                 withTitle:@"Test Banner"
                 usingFont:@"boldSystemFont"
               andFontSize:18];
    

    You are making two views – one with alloc/init, and one with your custom function. However, you are only ever assigning the first one to hView. This is because in the makeTableHeader method, you are using hView to create a second UIView, and applying the subviews/modifications to that rather than modifying hView. This view is then returned by the method, and then immediately discarded because it is not assigned to anything.

    Alternatively, if you insist on keeping it an instance method and modifying the view, you’ll just have to do it like this (although I strongly do not suggest it):

    -(void) makeTableHeader: (NSString *)ImageName 
                  withTitle:(NSString *)headerTitle 
                  usingFont:(NSString *)fontName 
                andFontSize:(CGFloat)fontSize {
         //you may want to remove all subviews here or something
         // Create the Image:
         self.frame = CGRectMake(0, 0, 320, 34);
         UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ImageName]];
         headerImageView.frame = CGRectMake(0, 0, 320, 34);
    
    
         // Now create the Header LABEL:
         UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 34)];
         headerLabel.text = headerTitle;
         headerLabel.font = [UIFont fontWithName:fontName size:fontSize];
         headerLabel.backgroundColor = [UIColor clearColor];
         headerLabel.textColor = [UIColor whiteColor];
         headerLabel.shadowColor = [UIColor blackColor];
         headerLabel.shadowOffset = CGSizeMake(1.0, 1.0);
    
         // Finally add both both Header and Label as subview to the main Header-view:
         [self addSubview:headerImageView];
         [self addSubview:headerLabel];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created an NSScanner category method that shows a leak in instruments. - (BOOL)scanBetweenPrefix:(NSString
I have created a PerformanceCounterCategory like below var category = PerformanceCounterCategory.Create(MyCat, Cat Help, PerformanceCounterCategoryType.SingleInstance,
I created a veriadic Category method on NSArray to filter it into a dictionary
having public ActionResult Create(CategoryViewModel viewModel) { if (!ModelState.IsValid) { return View(viewModel); } Category category
I have created a category for NSDate which can create a string representation of
Yes, I am still on Drupal 5. Don't make fun. I created a category
I want to allow any user to create a category using a text field.
in my noob mind and proceedings, when i create a category for a class,
I want to create a list of category terms (contained within a block), with
In PHP you can create form elements with names like: category[1] category[2] or even

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.