Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve layout for visionOS #593

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions Objective-C/TOCropViewController/TOCropViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

static const CGFloat kTOCropViewControllerTitleTopPadding = 14.0f;
static const CGFloat kTOCropViewControllerToolbarHeight = 44.0f;
#if TARGET_OS_VISION
static const CGFloat kTOCropViewControllerVisionCornerPadding = 16.0f;
#endif

@interface TOCropViewController () <UIActionSheetDelegate, UIViewControllerTransitioningDelegate, TOCropViewDelegate>

Expand Down Expand Up @@ -92,7 +95,7 @@ - (instancetype)initWithCroppingStyle:(TOCropViewCroppingStyle)style image:(UIIm
// Default initial behaviour
_aspectRatioPreset = TOCropViewControllerAspectRatioPresetOriginal;

#if TARGET_OS_MACCATALYST
#if TARGET_OS_MACCATALYST || TARGET_OS_VISION
_toolbarPosition = TOCropViewControllerToolbarPositionTop;
#else
_toolbarPosition = TOCropViewControllerToolbarPositionBottom;
Expand Down Expand Up @@ -277,9 +280,22 @@ - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures

- (CGRect)frameForToolbarWithVerticalLayout:(BOOL)verticalLayout
{
UIEdgeInsets insets = self.statusBarSafeInsets;

CGRect frame = CGRectZero;

#if TARGET_OS_VISION
frame.origin.x = kTOCropViewControllerVisionCornerPadding;
frame.size.width = CGRectGetWidth(self.view.bounds) - kTOCropViewControllerVisionCornerPadding * 2;
frame.size.height = kTOCropViewControllerToolbarHeight;

if (self.toolbarPosition == TOCropViewControllerToolbarPositionBottom) {
frame.origin.y = CGRectGetHeight(self.view.bounds) - (frame.size.height + kTOCropViewControllerVisionCornerPadding);
}
else {
frame.origin.y = kTOCropViewControllerVisionCornerPadding;
}
#else
UIEdgeInsets insets = self.statusBarSafeInsets;

if (!verticalLayout) { // In landscape laying out toolbar to the left
frame.origin.x = insets.left;
frame.origin.y = 0.0f;
Expand All @@ -297,6 +313,7 @@ - (CGRect)frameForToolbarWithVerticalLayout:(BOOL)verticalLayout
frame.origin.y = insets.top;
}
}
#endif

return frame;
}
Expand All @@ -319,6 +336,17 @@ - (CGRect)frameForCropViewWithVerticalLayout:(BOOL)verticalLayout
CGRect bounds = view.bounds;
CGRect frame = CGRectZero;

#if TARGET_OS_VISION
frame.size.height = CGRectGetHeight(bounds);
frame.size.width = CGRectGetWidth(bounds);

if (self.toolbarPosition == TOCropViewControllerToolbarPositionBottom) {
frame.size.height -= (kTOCropViewControllerVisionCornerPadding + kTOCropViewControllerToolbarHeight);
} else if (self.toolbarPosition == TOCropViewControllerToolbarPositionTop) {
frame.origin.y = kTOCropViewControllerToolbarHeight + kTOCropViewControllerVisionCornerPadding;
frame.size.height -= frame.origin.y;
}
#else
// Horizontal layout (eg landscape)
if (!verticalLayout) {
frame.origin.x = kTOCropViewControllerToolbarHeight + insets.left;
Expand All @@ -337,6 +365,7 @@ - (CGRect)frameForCropViewWithVerticalLayout:(BOOL)verticalLayout
frame.size.height -= frame.origin.y;
}
}
#endif

return frame;
}
Expand Down Expand Up @@ -409,6 +438,18 @@ - (void)adjustToolbarInsets
{
UIEdgeInsets insets = UIEdgeInsetsZero;

#if TARGET_OS_VISION
insets.left = kTOCropViewControllerVisionCornerPadding;
insets.right = kTOCropViewControllerVisionCornerPadding;

// Add padding on top if in vertical and tool bar is at the top
if (self.toolbarPosition == TOCropViewControllerToolbarPositionTop) {
insets.top = kTOCropViewControllerVisionCornerPadding;
}
else { // Add padding to the bottom otherwise
insets.bottom = kTOCropViewControllerVisionCornerPadding;
}
#else
if (@available(iOS 11.0, *)) {
// Add padding to the left in landscape mode
if (!self.verticalLayout) {
Expand All @@ -429,6 +470,7 @@ - (void)adjustToolbarInsets
insets.top = self.statusBarHeight;
}
}
#endif

// Update the toolbar with these properties
self.toolbar.backgroundViewOutsets = insets;
Expand Down Expand Up @@ -1248,7 +1290,7 @@ - (CGRect)imageCropFrame

- (BOOL)verticalLayout
{
#if TARGET_OS_MACCATALYST
#if TARGET_OS_MACCATALYST || TARGET_OS_VISION
return YES;
#endif

Expand Down Expand Up @@ -1295,14 +1337,16 @@ - (CGFloat)statusBarHeight
CGFloat statusBarHeight = 0.0f;
statusBarHeight = self.view.safeAreaInsets.top;

// We do need to include the status bar height on devices
// that have a physical hardware inset, like an iPhone X notch
BOOL hardwareRelatedInset = self.view.safeAreaInsets.bottom > FLT_EPSILON
&& UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone;
BOOL hardwareRelatedInset;

// Always have insetting on Mac Catalyst
#if TARGET_OS_MACCATALYST
// Always have insetting on Mac Catalyst and Apple Vision Pro
#if TARGET_OS_MACCATALYST || TARGET_OS_VISION
hardwareRelatedInset = YES;
#else
// We do need to include the status bar height on devices
// that have a physical hardware inset, like an iPhone X notch
hardwareRelatedInset = self.view.safeAreaInsets.bottom > FLT_EPSILON
&& UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone;
#endif

// Unless the status bar is visible, or we need to account
Expand Down