How to resize UITableView in UITableViewController with section header

后端 未结 5 1108
情话喂你
情话喂你 2020-12-29 13:14

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.

相关标签:
5条回答
  • 2020-12-29 13:40

    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]];
    
    0 讨论(0)
  • 2020-12-29 13:44

    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.

    0 讨论(0)
  • 2020-12-29 13:45

    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
    
    0 讨论(0)
  • 2020-12-29 13:46

    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
    
    0 讨论(0)
  • 2020-12-29 14:06

    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.

    0 讨论(0)
提交回复
热议问题