Skip to content

Commit be29621

Browse files
authored
Merge pull request #628 from MMP0/fix-toolbar-insets
Fix toolbar and title insets
2 parents 4f405ac + 2621cc0 commit be29621

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

Objective-C/TOCropViewController/TOCropViewController.m

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,16 @@ - (CGRect)frameForToolbarWithVerticalLayout:(BOOL)verticalLayout
340340
if (self.toolbarPosition == TOCropViewControllerToolbarPositionBottom) {
341341
frame.origin.y = CGRectGetHeight(self.view.bounds) - (frame.size.height + insets.bottom);
342342
} else {
343+
if (self.titleLabel.text.length) {
344+
// Work out the size of the title label based on the crop view size
345+
CGRect frame = self.titleLabel.frame;
346+
frame.size = [self.titleLabel sizeThatFits:self.cropView.frame.size];
347+
self.titleLabel.frame = frame;
348+
349+
// Set out the appropriate inset for that
350+
insets.top = CGRectGetMaxY(self.titleLabel.frame);
351+
insets.top += kTOCropViewControllerTitleTopPadding;
352+
}
343353
frame.origin.y = insets.top;
344354
}
345355
}
@@ -365,27 +375,24 @@ - (CGRect)frameForCropViewWithVerticalLayout:(BOOL)verticalLayout
365375
return view.bounds;
366376
}
367377

368-
UIEdgeInsets insets = self.statusBarSafeInsets;
369-
370378
CGRect bounds = view.bounds;
371379
CGRect frame = CGRectZero;
372380

373381
// Horizontal layout (eg landscape)
374382
if (!verticalLayout) {
375-
frame.origin.x = kTOCropViewControllerToolbarHeight + insets.left;
383+
frame.origin.x = CGRectGetMaxX(self.toolbar.frame);
376384
frame.size.width = CGRectGetWidth(bounds) - frame.origin.x;
377-
frame.size.height = CGRectGetHeight(bounds);
385+
frame.size.height = CGRectGetHeight(bounds);
378386
}
379387
else { // Vertical layout
380-
frame.size.height = CGRectGetHeight(bounds);
381388
frame.size.width = CGRectGetWidth(bounds);
382389

383390
// Set Y and adjust for height
384-
if (self.toolbarPosition == TOCropViewControllerToolbarPositionBottom) {
385-
frame.size.height -= (insets.bottom + kTOCropViewControllerToolbarHeight);
386-
} else if (self.toolbarPosition == TOCropViewControllerToolbarPositionTop) {
387-
frame.origin.y = kTOCropViewControllerToolbarHeight + insets.top;
388-
frame.size.height -= frame.origin.y;
391+
if (self.toolbarPosition == TOCropViewControllerToolbarPositionTop) {
392+
frame.origin.y = CGRectGetMaxY(self.toolbar.frame);
393+
frame.size.height = CGRectGetHeight(bounds) - frame.origin.y;
394+
} else {
395+
frame.size.height = CGRectGetMinY(self.toolbar.frame);
389396
}
390397
}
391398

@@ -424,40 +431,37 @@ - (void)adjustCropViewInsets
424431
insets.left = CGRectGetMaxX(self.toolbar.frame);
425432
} else {
426433
if (self.toolbarPosition == TOCropViewControllerToolbarPositionTop) {
427-
insets.top = CGRectGetMinY(self.toolbar.frame);
434+
insets.top = CGRectGetMaxY(self.toolbar.frame);
428435
} else {
429436
insets.bottom = CGRectGetHeight(self.view.frame) - CGRectGetMinY(self.toolbar.frame);
430437
}
431438
}
432-
}
433-
434-
// If there is no title text, inset the top of the content as high as possible
435-
if (!self.titleLabel.text.length) {
436-
if (self.verticalLayout) {
439+
} else {
440+
if (!self.verticalLayout) {
441+
insets.left = 0.0f;
442+
} else {
437443
if (self.toolbarPosition == TOCropViewControllerToolbarPositionTop) {
438-
self.cropView.cropRegionInsets = UIEdgeInsetsMake(0.0f, 0.0f, insets.bottom, 0.0f);
439-
}
440-
else { // Add padding to the top otherwise
441-
self.cropView.cropRegionInsets = UIEdgeInsetsMake(insets.top, 0.0f, insets.bottom, 0.0f);
444+
insets.top = 0.0f;
445+
} else {
446+
insets.bottom = 0.0f;
442447
}
443448
}
444-
else {
445-
self.cropView.cropRegionInsets = UIEdgeInsetsMake(0.0f, insets.left, insets.bottom, 0.0f);
446-
}
447-
448-
return;
449449
}
450450

451-
// Work out the size of the title label based on the crop view size
452-
CGRect frame = self.titleLabel.frame;
453-
frame.size = [self.titleLabel sizeThatFits:self.cropView.frame.size];
454-
self.titleLabel.frame = frame;
451+
if (!self.verticalLayout || self.toolbarPosition == TOCropViewControllerToolbarPositionBottom) {
452+
if (self.titleLabel.text.length) {
453+
// Work out the size of the title label based on the crop view size
454+
CGRect frame = self.titleLabel.frame;
455+
frame.size = [self.titleLabel sizeThatFits:self.cropView.frame.size];
456+
self.titleLabel.frame = frame;
457+
458+
// Set out the appropriate inset for that
459+
insets.top += self.titleLabel.frame.size.height;
460+
insets.top += kTOCropViewControllerTitleTopPadding;
461+
}
462+
}
455463

456-
// Set out the appropriate inset for that
457-
CGFloat verticalInset = self.statusBarHeight;
458-
verticalInset += kTOCropViewControllerTitleTopPadding;
459-
verticalInset += self.titleLabel.frame.size.height;
460-
self.cropView.cropRegionInsets = UIEdgeInsetsMake(verticalInset, insets.left, insets.bottom, 0);
464+
self.cropView.cropRegionInsets = insets;
461465
}
462466

463467
- (void)adjustToolbarInsets
@@ -1100,7 +1104,7 @@ - (UILabel *)titleLabel
11001104
_titleLabel.textAlignment = NSTextAlignmentCenter;
11011105
_titleLabel.text = self.title;
11021106

1103-
[self.view insertSubview:self.titleLabel aboveSubview:self.cropView];
1107+
[self.view insertSubview:self.titleLabel aboveSubview:self.toolbar];
11041108

11051109
return _titleLabel;
11061110
}

0 commit comments

Comments
 (0)