56

特定のサイズのモーダルビューコントローラーがいくつかあります。私のサイズに合ったmodalPresentationStyleがないため、カスタムビュー(現在のビューに全画面の黒い半透明のオーバーレイを作成する、そのビューにモーダルビューを追加する、アニメーションを実行するなど)の使用を避けようとしていますコントローラー。

現在、UIModalPresentationPageSheet を使用していますが、ビューの高さが小さく、醜い空白スペースがあります。

希望するプレゼンテーション

 _______________
|    _______    |
|   |       |   |
|   |  MyVC |   |
|   |       |   |
|    -------    |
 --------------- 

実際のプレゼンテーション

 _______________
|   |       |   |
|   |  MyVC |   |
|   |       |   |
|   |-------|   |
|   | blank |   |
 ---------------    

UIModalPresentationFormSheet を使用すると、コンテナーの幅が小さくなります。

どうすればよいか考えているのですが、可能かどうかわかりません。どのプレゼンテーション スタイルよりも小さいモーダル VC を提示するという問題の解決策は何ですか? 唯一の解決策は、「カスタム モーダル ビュー コントローラー エンジン」を配置することですか? ポップオーバーは私の設計要件に適合しません:(

4

13 に答える 13

84

ここにいる他のユーザーと同様に、モーダルビューコントローラーの起源が正しくないという問題もありました。いくつかの実験の後、私は自分に合った解決策を見つけました:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];   
    self.view.superview.bounds = CGRectMake(0, 0, <width>, <height>);
}
于 2013-01-23T19:39:45.747 に答える
35

以下を使用して、次の結果 (リンク テキスト)を取得しました。

self.modalPresentationStyle = UIModalPresentationFormSheet;

モーダルView Controllerとして提示することによって。さらにサポートが必要な場合はお知らせください。

于 2010-08-02T08:24:42.637 に答える
24

これらの回答はすべて、ios8 SDK では機能しません。

これはうまくいきます:

AboutViewController * _aboutViewController = [[AboutViewController alloc] init];
    _aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    if(IS_IOS8)
    {
        _aboutViewController.preferredContentSize = CGSizeMake(300, 300);
    }
    [self presentViewController:_aboutViewController animated:YES completion:nil];

AboutViewController.m で

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

    if(!IS_IOS8)
    {
        self.view.superview.bounds = CGRectMake(0, 0, 300, 300);
    }
}

IS_IOS8

#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)

iOS 8 では、より多くのカスタマイズ オプションを提供する UIPresentationController も使用できます。

于 2014-09-22T07:43:01.003 に答える
19

最初に何ViewController.view.superview.frameが設定されているかを理解するまで、カスタムサイズのモーダルを中央に配置するのに問題がありました。UIModalPresentation タイプに基づいて決定されます。superview.center必要さえありません(私のテストが示す限り)。

また、 を使用して座標を動的に設定し、[UIScreen mainScreen].applicationFrame現在は横向きのみをサポートしています。X/Y および高さ/幅の値を反転して、縦向きと横向きの両方をサポートするには、条件付きチェックを行う必要があります。

iOS 6.0 の最初の実用的なソリューションは次のとおりです。

ViewController.modalPresentationStyle = UIModalPresentationFormSheet;
ViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:ViewController animated:YES completion:nil];
ViewController.view.superview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
ViewController.view.superview.frame = CGRectMake(
                                                                    // Calcuation based on landscape orientation (width=height) 
                                                                    ([UIScreen mainScreen].applicationFrame.size.height/2)-(320/2),// X
                                                                    ([UIScreen mainScreen].applicationFrame.size.width/2)-(320/2),// Y
                                                                    320,// Width
                                                                    320// Height
                                                                    );

そして先程の回答を見て、顔をしかめた。最後の行を次のように置き換えることで、ソリューションを少し短縮します。

ViewController.view.superview.bounds = CGRectMake(0, 0, 320, 320);

最終的な解決策とコメントを編集します。

于 2012-11-12T19:58:25.477 に答える
18

これを行う最善の方法は、フォームシート内に表示されているビューのスーパービューで境界プロパティを変更することです。ビューをモーダルに表示すると、表示されたビューは別のビュー内に埋め込まれます。

スーパービューの境界プロパティをビューの境界と同じ四角形に設定する必要があります。まず、クラスにプライベート ivar を追加します。

@implementation MySpecialFormsheet {
    CGRect _realBounds;
} 

でこれを行うのが最善ですviewWillAppear:。モーダルに表示されているビュー コントローラーに次のコードを追加します。

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

メソッドにキャッシュする必要があり_realBoundsます。viewDidLoad

- (void)viewDidLoad {
    _realBounds = self.view.bounds;
    [super viewDidLoad];
}
于 2012-04-13T03:08:18.117 に答える
7

これは、自動レイアウト ビューではない (自動レイアウトで試したことがない) ための私のソリューションであり、iOS 7 および iOS 8 に準拠しています。

最初に、必ず次の方法でモーダル ビューを呼び出してください。

CloudSettingsViewController *cloudController = [[CloudSettingsViewController alloc] initWithNibName:@"CloudSettingsView" bundle:nil];

CGRect originalBounds = cloudController.view.bounds;

cloudController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
cloudController.modalPresentationStyle = UIModalPresentationFormSheet;

[self presentViewController:cloudController animated:YES completion:nil]; 

iOS 8 では、モーダル ビューの viewDidLoad メソッドで preferredContentSize を設定します。

- (void)viewDidLoad {
    [super viewDidLoad];

    // backup of the original size (selected in interface builder)
    height = self.view.frame.size.height;
    width = self.view.frame.size.width;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) // versioni successive ios 8
        self.preferredContentSize = CGSizeMake(width, height); // this is necessary to get the correct size of the modal view
}

これは、モーダル ビューで iPad にキーボードを表示する必要がある場合にも機能します。

以前のバージョンの iOS 8 では、presentViewController 呼び出しの後に境界を設定する必要があります。

[currentController presentViewController:controlPanelController animated:YES completion:nil];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) 
    cloudController.view.superview.bounds = originalBounds;//it's important to do this after 
于 2014-09-24T12:56:06.857 に答える
7

使用できる用紙シートの高さと幅を縮小する場合でも

[self.view.superview setBounds:CGRectMake(0, 0, 450, 480)];
于 2012-10-11T05:25:03.260 に答える
4

私でさえ、かなり長い間同じ問題に苦しんでいました。ここで提供された多くの回答からいくつかの試行を行った後、私にとって非常にうまく機能する解決策を思いつきました。

ビューコントローラーを表示する際のコードは次のとおりです。

    SomeViewController *sovcObj = [[SomeViewController alloc] initWithNibName:@"SyncOptionsViewController" bundle:nil];
someObj.modalPresentationStyle = UIModalPresentationFormSheet;
someObj.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:someObj animated:YES completion:nil];
someObj.view.superview.bounds = CGRectMake(0, 0, 400, 400); // whatever width and height you want

'Simulated Metrics' セクションの xib (使用している場合) で、'Freeform' サイズを選択します。

これとは別に、提示されたビュー コントローラー (つまり、この例では SomeViewController) に以下のコードを記述する必要があります。

- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.view.superview.bounds = CGRectMake(0, 0, 400, 400); // whatever width and height you want

}

このコードは iOS 7 でも機能します

于 2014-01-21T12:11:24.807 に答える
3

1 つの解決策は、UIModalPresentationPageSheet を使用してページ シートを表示し、すぐにモーダル ビューのサイズを変更することです。これは、別の質問に対するこの回答で完全に説明されています。

于 2011-02-08T17:11:47.583 に答える
1

presentModalViewController メソッドを使用して表示された「後に」モーダル ビューのサイズを変更します。モーダル ビューのサイズを変更するには、このリンクを参照してください https://coderwall.com/p/vebqaq

于 2013-05-02T14:41:45.787 に答える
1

次のようにMZFormSheetControllerを使用できます。

MZFormSheetController *formSheet = [[MZFormSheetController alloc] initWithSize:customSize viewController:presentedViewController];
[presentingViewController mz_presentFormSheetController:formSheet animated:YES completionHandler:nil];
于 2015-01-22T11:02:23.173 に答える
0

私は同じ問題を抱えていたので、この解決策は私にとってはうまくいきました。

私のモーダルビュー構造は次のとおりです(提示されたVCで透明性を実現するために、提示されたコントローラーと提示されたコントローラーの両方でUIModalPresentationCurrentContextを使用します)

ModalViewController
  > UIView (view) - resized to the same size as the presenting controller but has semi-translucent background - done automatically
      >> UIView (contentView) - the true view with the content of the popup - this is the one that got consistently placed at the top of the screen all the time

ModalViewController では、配置の問題を修正するために次のメソッドを上書きしました。

- (void) viewWillLayoutSubviews(){
  [super viewWillLayoutSubviews];

  CGRect r = self.view.bounds();
  self.contentView.center = CGPointMake(r.size.width/2,r.size.height/2);
}

最初に実際にコンテンツビューを作成してサイズを変更しますが、方法は異なります。

これが誰かに役立つことを願っています。

于 2015-01-21T19:50:08.047 に答える