Dynamic view's height depends on table view's height?

1.6k views Asked by At

Now I'm working with CWPopup category (https://github.com/cezarywojcik/CWPopup) and I've got a problem with layout: I created a tableView within a popup view, call this popup view from my mainView, expected result is popupView.height = navigationBar.height + tableView.height ( tableView.height is dynamic ), but it doesn't work as expected. Now it looks like that:

Table view with max item

and

Table view with min item I've tried auto-layout and auto-resizing mask but it didn't help. Any body can show me a better solution?

3

There are 3 answers

0
Fahad On

Height of the tableview won't change dynamically. you can use the number of rows and height of the cell to calculate frame height and set it explicitly.

0
Zhang On

If you're presenting a modal view controller as a form sheet, then need to do something like this in your viewWillLayoutSubviews method:

- (void)viewWillLayoutSubviews
{

    ...

    if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        self.view.superview.bounds = CGRectMake(0, 0, 540.0, requiredHeight);
    }
    else
    {
        self.view.bounds = CGRectMake(0, 0, 540.0, requiredHeight);
    }
}

Can't remember where I got this code from:

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

But I would put this in a header file and import into your project.

0
Jack On

Use viewDidLayoutSubviews & set UITableview frame

override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            tableViewWhatsHot.frame = CGRect(x: tableViewWhatsHot.frame.origin.x, y: tableViewWhatsHot.frame.origin.y, width: tableViewWhatsHot.frame.size.width, height: tableViewWhatsHot.contentSize.height)
      }