我同意@PeterPurple 的回答,但我花了一些时间才想出真正有效的代码。我的示例显示了一个水平居中的模态视图,但更多地位于屏幕顶部,因此视图不会阻碍键盘。当然,当键盘显示时,我可以将视图向上移动,但我很懒。无论如何,这就是我的做法。
我创建了一个自定义模态序列,并在该序列中覆盖了 perform: 方法,如下所示:
@implementation CustomSegue
static const CGFloat TOP_MARGIN = 40;
static const CGFloat SUPER_VIEW_GAP = 10;
- (void)perform {
UIView *destView = [self.destinationViewController view];
[self.destinationViewController setModalPresentationStyle:UIModalPresentationPageSheet];
[self.destinationViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self sourceViewController] presentViewController:[self destinationViewController] animated:YES completion:nil];
CGFloat x, y, width, height, yCenter;
CGSize contentSize = [self.destinationViewController contentSizeForViewInPopover];
x = destView.superview.frame.origin.x;
y = destView.superview.frame.origin.y;
width = contentSize.width;
height = contentSize.height;
yCenter = (height / 2.0) + TOP_MARGIN;
destView.superview.frame = CGRectMake(x + SUPER_VIEW_GAP, y + SUPER_VIEW_GAP, width - (SUPER_VIEW_GAP * 2), height - (SUPER_VIEW_GAP * 2));
destView.superview.autoresizingMask = UIViewAutoresizingNone;
destView.superview.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
destView.superview.center = CGPointMake([[self.sourceViewController view] center].x, yCenter);
destView.frame = CGRectMake(-SUPER_VIEW_GAP, -SUPER_VIEW_GAP, width, height);
}