Orderella / PopupDialog

A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.
http://www.mwfire.de
Other
3.94k stars 520 forks source link

Portrait image causing popup to get cut off and go out of view #97

Closed LaChrome closed 7 years ago

LaChrome commented 7 years ago

Report

Environment

Please provide information on your development environment, so we can build with the same scenario.

Dependency management

If you are not using any dependency managers, you can remove this section.

Please note: If you are using CocoaPods with Xcode 8, CocoaPods 1.1.0 is required.

What did you do?

Initialized PopupDialog with a portrait UIImage (see code at very bottom of this report)

What did you expect to happen?

Expected image / Popup to scale proportionally so that all buttons show and popup fits in view.

What happened instead?

Popup goes out of view, and doesn't show bottom cancel button (below photo button). See attached screenshot. I've used multiple portrait photos, and this issue happens for each photo used.

img_0976 png

Project that demonstrates the issue

Code: NSString previewPath = [[NSHomeDirectory() stringByAppendingString:self.currentProject.directoryName] stringByAppendingPathComponent:@"preview.png"]; CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:previewPath]); CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); UIImage previewImage = [[UIImage alloc] initWithCGImage:image];

PopupDialog *popup = [[PopupDialog alloc] initWithTitle:@"Create & Share"
                                                message:nil
                                                  image:previewImage
                                        buttonAlignment:UILayoutConstraintAxisHorizontal
                                        transitionStyle:PopupDialogTransitionStyleBounceUp
                                       gestureDismissal:YES
                                             completion:nil];

[popup setButtonAlignment:UILayoutConstraintAxisVertical];
[[PopupDialogDefaultView appearance] setBackgroundColor:UINavColor];

DefaultButton *video = [[DefaultButton alloc] initWithTitle:@"Video" height:50 dismissOnTap:TRUE action:^{
}];

DefaultButton *gif = [[DefaultButton alloc] initWithTitle:@"Animated GIF" height:50 dismissOnTap:TRUE action:^{
}];

DefaultButton *photo = [[DefaultButton alloc] initWithTitle:@"Photo" height:50 dismissOnTap:TRUE action:^{
}];

CancelButton *cancel = [[CancelButton alloc] initWithTitle:@"Cancel" height:50 dismissOnTap:TRUE action:^{

}];

[popup addButtons: @[video, gif,photo,cancel]];
[self presentViewController:popup animated:YES completion:nil];
LaChrome commented 7 years ago

Here is a quick hack / fix to get the portrait image to work (resize with aspect fit):

CGRect scaledImageRect = CGRectZero;

CGSize newSize = CGSizeMake(300, 300);
CGFloat aspectWidth = newSize.width / previewImage.size.width;
CGFloat aspectHeight = newSize.height / previewImage.size.height;
CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight );

scaledImageRect.size.width = previewImage.size.width * aspectRatio;
scaledImageRect.size.height = previewImage.size.height * aspectRatio;
scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f;
scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f;

UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 );
[previewImage drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
mwfire commented 7 years ago

Portrait images are indeed a problem when displayed in a dialog. This is also true for large amounts of text, even more so on smaller devices like the iPhone 4S and landscape orientation. Therefore, it is your responsibility to make sure the dialog does not exceed the viewport in any of those cases, as the standard PopupDialog does not know about your intentions.

Having said that, if you need a custom solution like scroll views to handle these situations, you can always use a custom view controller with PopupDialog.