How To Dynamically change the contentSize of UIPopoverController?

后端 未结 8 456
执念已碎
执念已碎 2020-12-14 16:35

I have a UIViewController that contains a UITableView. This UIViewController is being displayed in a UIPopoverController.

相关标签:
8条回答
  • 2020-12-14 17:00

    My app has a menu with sub-menus originating from a popover and embedded in a navigation controller, i.e. something like UINavigationController -> TopMenuViewController -> SubMenuViewControllerxN.

    None of the above solutions worked for me.

    My solution: In my root menu view controller class, from which all other menu view controllers inherit, add the following code:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationController.preferredContentSize  = CGSizeMake(450.0f, 0.0f);// increase standard width (320.0)
    }
    
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        self.navigationController.preferredContentSize = self.tableView.contentSize;
    }
    
    0 讨论(0)
  • 2020-12-14 17:02

    A UIViewController class has the property

    self.contentSizeForViewInPopover
    

    which will resize the pop over w/out needing to adding a reference to it.

    And to expand on a solution, i used the method rectForSection: to get the size of the section (mine only has 1 section, so easy enough to get) and then added the height of the navigation bar (it seems to be 20). so i'm able to create the popover the size of the finished table view:

    CGRect sectionRect = [view.tableView rectForSection:0];
    
    if (sectionRect.size.height + 20 < POPOVER_SIZE.height)
        view.contentSizeForViewInPopover = CGSizeMake(POPOVER_SIZE.width, sectionRect.size.height + 20);
    else
        view.contentSizeForViewInPopover = POPOVER_SIZE;
    

    might prove more difficult with multiple sections, i didn't try it. should just be able to sum up the section heights, but there might be some spacing issues that i don't know about.

    0 讨论(0)
  • 2020-12-14 17:04

    For iOS 7 or above.

    - (CGSize)preferredContentSize {
        return CGSizeMake(320, 550);
    }
    

    If you are a child of a container, re-direct the content size to yourself. E.g. In a UINavigationController subclass:

    - (CGSize)preferredContentSize {
        return self.topViewController.preferredContentSize;
    }
    
    0 讨论(0)
  • 2020-12-14 17:07

    This should do the trick

    override func viewWillLayoutSubviews() {
      super.viewWillLayoutSubviews()
    
      self.preferredContentSize = CGSizeMake(0, self.tableView.contentSize.height)}
    
    0 讨论(0)
  • 2020-12-14 17:08

    iOS8/9 solution - just override preferredContentSize and force the table view to layout prior to returning it's contentSize.

    - (CGSize)preferredContentSize {
        [self.tableView layoutIfNeeded];
        return self.tableView.contentSize;
    }
    
    0 讨论(0)
  • 2020-12-14 17:13

    I might be very late to answer but for new user from iOS 7 please use the following line in your UIViewController i,e contentViewController of your UIPopOverViewConotroller

    -(void) viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        self.preferredContentSize=myTableView.contentSize;
    }
    

    Hope this will help for iOS 7 user.

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