Rickye
Rickye

Reputation: 1021

How to resize UITableView in UITableViewController with section header

I've got a grouped UITableView in a UITableViewController and I want to resize it horizontally.

I tried many different ways but none of them was perfect.

What I've tried:

1.) Overriding - [UITableView setFrame:], but it didn't move the headers of the sections and there are black areas on both sides (because there isn't anything behind the table view).

2.) Overriding - [UITableViewCell setFrame:], but it still doesn't move the headers (which is important).

3.) Calling - [self.view setFrame:] from UITableViewController, but it doesn't do anything.

If you've got any idea how to solve it please share it with me!

Upvotes: 9

Views: 21070

Answers (5)

Muvimotv
Muvimotv

Reputation: 893

Bit late to the party but it can always come handy for others searching for the topic...

I don't think doing that in the viewDid appear is the right way to do it since it will definitely be visible to the user and resize in front of their eyes. Doesn't make for a great user experience imo.

In swift 4 I use something like

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    var rect = tableView.frame
    if rect.size.height != h && rect.size.width != w { 
        // set x and y to any value you need. you can also have a 
        // condition about the origin (x,y) 
        // if you have a gap on the left or top...
        rect.size.height = h;
        rect.size.width = w;
        tableView.frame = rect
    }
}

This will make the changes before the tableView is visible and make the user experience so much better and also as mentioned in the accepted answer you will need to set the window color the color of your table view to blend better (white being the default)

    UIApplication.shared.keyWindow?.backgroundColor = .white

Upvotes: 0

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 2451

I know this is old question but I think in this scenario it is better to use UITableViewController embedded in Container view instead of resizing UITableViewController's tableView.

Upvotes: 2

user529758
user529758

Reputation:

If you call - [UITableView setFrame:] from - [UITableViewController viewDidAppear:], it works:

- (void)viewDidAppear:(BOOL)animated
{
    [self.tableView setFrame:CGRectMake(x, y, w, h)];
}

In order to avoid having black bars on each side of the table view, set the background color of the application's main window to white:

[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];

Upvotes: 37

Joe Fratianni
Joe Fratianni

Reputation: 790

To fix the headers being resized I would try:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGFloat headerHeight = 40;
    UIView *headerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, headerHeight)];
    UILabel *cellLabel = [[UILabel alloc] initWithFrame: headerView.frame];
    [cellLabel setText: @"My Text"];
    [cellLabel setBackgroundColor: [UIColor clearColor]];
    [headerView addSubview: cellLabel];
    return headerView;
}

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

This code should replace this method:

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

Upvotes: 2

rmaddy
rmaddy

Reputation: 318794

The main issue is that the table view of a UITableViewController is the main view so it's not meant to be resized.

The best option is not to use UITableViewController. Instead, use UIViewController and add your own UITableView as a subview of the view controller's main view. This way you can size as needed.

Of course there is extra work to hook up all of the plumbing so your view controller works like a table view controller but there isn't too much to do.

Upvotes: 14

Related Questions