3

I have a popover controller i instantiate in code and pass it it's contentViewController, and all that works great. The problem is LATER when the contentViewController might resize as a result of the messages label displaying an arbitrarily long list of strings.

The UILabel I'm referring to has the text ... 'Validation and Errors Message Box'

I'd like to be able to somehow configure the layout constraints so that when the user enters a value in the textbox, and then taps the accept button, that if I have any validation errors to present to them, I can do so by adding them to the label (which I have set up to accept multiple lines of data if needed), and when added, if they happen to require 5 lines of display, that the label will then push down the UITextField and the Accept/Cancel buttons so that it can display all the label content. Well, I've been able to do that, and the contentViewController resizes as expected. The problem is, the popover controller that contains it DOES NOT resize, so what happens is, my Accept/Cancel buttons get pushed off the bottom edge of the popover form so they aren't even accessible to the user.

Are there any constraints I can set up that will force the height of the popover controller to enlarge so that the UI controls under my 'Validation and Errors message box' aren't pushed off the bottom edge when there are multiple lines of validation text?

I'm creating the popoverController in code, so I'm assuming I'd need to do the contraint(s) in code as well.

Screenshot

5
  • You probably should go through intrinsic resizing tutorials with auto layout. .... That and just flat out auto layout tutorials. This is a simple matter of configuring the proper constraints. Very basic stuff. Commented May 28, 2015 at 1:16
  • 1
    Well since I'm passing this prebuilt/presized contentViewController to a popover, which determines its size at creation, my guess is that the popover controller will not be resized dynamically after creation even if the contentViewController is. I don't think it's as basic as you claim it is. Commented May 28, 2015 at 1:31
  • Subviews are friends... Commented May 28, 2015 at 1:33
  • Also, it's insanely easy to roll your own via childviewcontrollers if you don't like the current popovers restrictions Commented May 28, 2015 at 1:38
  • 1
    I think you'll have to do this by making your contentViewController as tall as it will ever need to be, but give it a clear background color with the gray view you show as a subview -- that subview should be able to grow with the label.
    – rdelmar
    Commented May 28, 2015 at 2:53

3 Answers 3

7

Here's how I solved it, and it wasn't using constraints. Everything I could possibly find about using AutoLayout and constraints had no useful constraint solution.

As I create the popover, and before displaying it, I use this code...

CGSize newSize = [theContentViewController.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
CGRect newFrame = CGRectMake(0, 0, newSize.width, newSize.height);
[theContentViewController.view setFrame:newFrame];

[thePopover setPopoverContentSize:theContentViewController.view.frame.size animated:YES];

Then, after the user presses the Accept button, within the contentViewController, where I evaluate whether their input is acceptable or not, and update the label with any messages, I then call the same code above, which will animate to the new size to fit a large message or whatever.

1

2018 answer: Put this in the ContentViewController

override func viewDidLoad() {
    super.viewDidLoad()
    preferredContentSize = view.frame.size
}
1
  • That is rarely going to work: it assumes you have an absolutely static layout in self.view
    – xaphod
    Commented Nov 29, 2018 at 16:17
0

Assuming we have used Auto Layout to fully constrain our content we can calculate the fitting size and use that to size the popover. In the viewDidLoad method of my content view controller:

preferredContentSize = view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)

Using systemLayoutSizeFitting on the root view of the view controller asks the Auto Layout engine to calculate the smallest (layoutFittingCompressedSize) size that will fit the layout:

Ref: https://useyourloaf.com/blog/self-sizing-popovers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.