Search code examples
objective-cipaduipopovercontroller

Dynamic UIPopoverController Size?


I have a UIPopoverController in my app which simply displays two UILabels beside each other with a list of words in each of them. However sometimes there are only a couple of words in each list meaning there is tons of blank space in the popover view.

How can I make it so that the popover view in at least height dynamically adapts to how many lines of words there are in my label?


Solution

  • If text in label is specified before popover shows, you can achieve this by using similar code in viewDidLoad method:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // ...
    
        CGFloat height = [label.text sizeWithFont:label.font 
                                         forWidth:label.frame.size.width 
                                    lineBreakMode:label.lineBreakMode].height;
    
        // This calculates only height of the label, you may want to add some margins, etc. 
    
        CGSize size = CGSizeMake(self.view.frame.size.width, height);
        self.contentSizeForViewInPopover = size;
    }