【问题标题】:Centering a custom sized UIModalPresentationPageSheet?居中自定义大小的 UIModalPresentationPageSheet?
【发布时间】:2013-01-14 18:14:15
【问题描述】:

我们可以使用以下代码(如下)创建非标准尺寸的表单视图,但生成的视图不是居中的——x 坐标始终是标准尺寸的表单视图所在的位置。更改视图和超级视图的中心属性不会以有用的方式影响任何内容。我们如何使用正确居中的自定义表单大小?

将以下代码添加到显示为UIModalPresentationPageSheet 的视图控制器:

@implementation MySpecialFormsheet {
    CGRect _realBounds;
} 

- (void)viewDidLoad {
    // code below works great, but the resulting view isn't centered.
    _realBounds = self.view.bounds;
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.superview.bounds = _realBounds;
}

【问题讨论】:

    标签: ios ipad cocoa-touch ios6


    【解决方案1】:

    我只是在展示后才发现更改框架成功。我实际上是这样做的:

    GameSetupViewController *gameSetup = [[GameSetupViewController alloc] init];
    
    [gameSetup setDelegate:self];
    
    [gameSetup setModalPresentationStyle:UIModalPresentationPageSheet];
    
    [self presentModalViewController:gameSetup animated:YES];
    
    [gameSetup.view.superview setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
    [gameSetup.view.superview setFrame:CGRectMake(64, 64, 896, 640)];
    

    但也许您可以使用 viewDidAppear 或其他一些句柄来侥幸。

    【讨论】:

    • 感谢您的回复。 presentModalViewController 在 iOS6 中已贬值,我的 VC 正在通过故事板呈现。不过,如果没有其他方法可以解决这个问题,我可能最终会使用这个解决方案,所以谢谢!
    • iOS6 中的 presentViewController 也应该如此。刚刚从我目前打开的项目中获取了这段代码。无论哪种方式+1都是一个好问题。希望有一种更好的方式,那就是封装在视图本身中。甚至更好的方法来创建可应用于任何视图的自定义 ModalPresentationStyles。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2014-10-16
    • 2019-04-20
    相关资源
    最近更新 更多