Search code examples
iosautolayoutuipopover

How do I size a popover to the size of the contentview?


I have a popover which shows a UIViewController containing a UIView and a UILabel. I want the popover to grow/shrink to the size of the content of the UILabel. How do I do that? Here is the part of the UIViewController in the popover which now determines it's size...

#pragma mark - View controller life cycle

- (CGSize)preferredContentSize {
    return self.view.frame.size;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    _personNameLabel.text = self.person.fullNameWithTitle;
}

What do I need to do to have the popover to show the person's name and size itself to the size of the name?


Solution

  • Calculate width and height and set this,

    For iOS 6,

    self.contentSizeForViewInPopover = CGSizeMake(width,height);
    

    For iOS7,

    self.preferredContentSize = CGSizeMake(width,height); 
    

    set this in viewDidLoad or in viewWillAppear method.