Search code examples
iospopover

How to make a UIView or TableView to fit the preferredContentSize of a popover?


I wanna show a popover with a 100X100 imageView or tableView. Usually I have to create an imageView on the corner Of the popoverView,like this.
(http://snappyimages.nextwavesrl.netdna-cdn.com/img/c7533f30a6ebef7c017521fd8f84452e.png)

But if I change the the preferredContentSize,I have to change the the size of imageView too.

Q1:
Is it the right way to create the content of a popover ?

Q2:
Is there a way to make the imageView or tableView to fit the preferredContentSize automatically?


Solution

  • I've got the anwser!

    Impelement code in presentating controller.

    popoverController. preferredContentSize = popoverControoler.yourTableView.contentSize;
    popoverController. preferredContentSize = popoverControoler.yourImageView.frame.size;

    or
    implement the code in popoverController (make sure yourView has been laid out)

    -(void) viewDidAppear:(BOOL)animated{ self.preferredContentSize=yourTableView.contentSize; }

    -(void) viewDidAppear:(BOOL)animated{ self.preferredContentSize=yourImageView.contentSize; }