m1entus / MZFormSheetPresentationController

MZFormSheetPresentationController provides an alternative to the native iOS UIModalPresentationFormSheet, adding support for iPhone and additional opportunities to setup UIPresentationController size and feel form sheet.
MIT License
974 stars 145 forks source link

Exact size calculation when using UILayoutFittingCompressedSize #123

Closed roccozanni closed 7 years ago

roccozanni commented 7 years ago

This PR address an issue I found when using UILayoutFittingCompressedSize to dynamically size a popup with multiline labels. The popup had a fixed with constraint and I wanted to calculate the height dynamically to fit exactly fit the text.

This is what I got when I set the presentationController.contentViewSize attribute to UILayoutFittingCompressedSize. As you can see the calculated height is not exact (look at the bottom part of the popup):

img_0549

By forcing autolayout to run before invoking [contentView systemLayoutSizeFittingSize: contentViewSize] the view is then measured correctly and that sizing issue disappeared.

roccozanni commented 7 years ago

Sorry @m1entus, after I submitted the PR I noticed this in the XCode log:

Disclaimer: this was present also before I made the PR, I just didn't noticed it, so it's not related to PR per-se, but at this stage I'm not sure the addition was really needed or the miscalculation was somewhat a side-effect of the constraint issue.

(
    "<NSLayoutConstraint:0x17429ed70 UIView:0x102e05e30.width == 280   (active)>",
    "<NSLayoutConstraint:0x17029ff90 'UIView-Encapsulated-Layout-Width' UIView:0x102e05e30.width == 284   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17029ff90 'UIView-Encapsulated-Layout-Width' UIView:0x102e05e30.width == 284   (active)>

I'd like to understand what is the correct way to handle my original problem, and then we'll see if this PR is still needed. This is my code to create the popup:

SPKPopupViewController * controller = [[SPKPopupViewController alloc] init];
MZFormSheetPresentationViewController * formSheetController = [[MZFormSheetPresentationViewController alloc] initWithContentViewController:controller];
formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyleFade;
formSheetController.presentationController.shouldDismissOnBackgroundViewTap = NO;
formSheetController.presentationController.shouldCenterVertically = YES;
formSheetController.presentationController.shouldCenterHorizontally = YES;
formSheetController.presentationController.contentViewSize = UILayoutFittingCompressedSize; 

then, in order to make the width fixed, and the height dynamic, I added this constraint on SPKPopupController.viewDidLoad:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                    multiplier:1.0
                                                      constant:280.0]];

Based on what I see in the code:

Sorry to bother you man, I still have some struggle to perfectly understand how Autolayout works under the hood.

Thanks

m1entus commented 7 years ago

Generally this feature should be changed, i have some changes in different project, but don't have motivation and time to move it, clean it up now :/ Hope soon will come good time and will do all of it.