Here is a short and sweet solution.
The first step, in initializing the UITableViewController, add:
UILabel *placeholder = [[UILabel alloc] init]; placeholder.font = [placeholder.font fontWithSize:20]; placeholder.numberOfLines = 0; // Use as many lines as needed. placeholder.text = NSLocalizedString(@"Your text here", nil); placeholder.textAlignment = NSTextAlignmentCenter; placeholder.textColor = [UIColor lightGrayColor]; placeholder.hidden = YES; // Initially hidden. [self.tableView addSubview:placeholder]; self.placeholder = placeholder; // You'll need a reference to the placeholder.
The second step, in your UITableViewController, add:
- (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; self.placeholder.frame = self.tableView.bounds; }
Last step, show / hide the placeholder if necessary.
A better solution would not be to use a UITableViewController. Just add a tableView to the UIViewController and add a placeholder view to the UIViewController above the table view. Use the automatic layout so that the placeholder is centered vertically and horizontally. Finally, show / hide the placeholder, if necessary.
What do you think of this decision? Please comment.
source share