2013-05-13 24 views
25

我試圖在表單表單演示文稿中加載在iPad的UIViewController。問題是這個新視圖的大小,我把尺寸值放在IBuilder中,但是模態視圖取一個固定的值。Modal View自定義大小與表單表單演示

此外,我試圖讓這prepareForSegue這樣的:

HelpViewController *viewController = segue.destinationViewController; 
viewController.view.superview.frame = CGRectMake(0, 0, 200, 200); 

但不工作,任何幫助嗎?謝謝!

+0

您好,我得到設置在使用該模態的視圖的方法viewWillAppear中一個自定義的尺寸: [self.view.superview的setBounds:CGRectMake(0,0, 200,200)]; 但是現在,該窗口並沒有以佈局 – 2013-05-13 10:05:06

+1

爲中心(http://stackoverflow.com/questions/2457947/how-to-resize-a-uipresentationformsheet/4271364#4271364) – 2013-05-13 10:25:16

+0

http:// stackoverflow.com/questions/25787946/ios8-unable-to-re-size-the-modal-form-sheet-after-changing-the-orientation 檢查此鏈接,你可以得到解決 – 2014-12-09 12:51:43

回答

0

我也有這個問題,您應該在呈現它之後調整superview的框架大小。

HelpViewController *viewController = segue.destinationViewController; 
viewController.modalPresentationStyle = UIModalPresentationPageSheet; 
[self presentViewController:viewController animated:YES completion:nil]; 
viewController.view.superview.frame = CGRectMake(0, 0, 200, 200); 
+2

這也doenst工作 – Ranjit 2013-10-23 07:14:24

6

可以在中心

HelpViewController * viewController = [[[HelpViewController alloc] init] autorelease]; 
viewController.modalPresentationStyle=UIModalPresentationFormSheet; 
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentViewController:viewController animated:YES completion:^{ 
    viewController.view.superview.frame = CGRectMake(0, 0, 200, 200); 
    viewController.view.superview.center = self.view.center; 
}]; 
+2

至少在iOS 7中這不起作用 - 它顯示它呈現如果全尺寸,然後它縮小它。正如「完成」塊所證明的那樣。 – iOSProgrammingIsFun 2013-10-20 14:47:45

+0

@iOSProgrammingIsFun:它在ios7中工作。 – 2013-12-05 05:43:47

+2

要在iOS7執行此採取一看這個http://stackoverflow.com/a/19026882/1363634 – CoderPug 2014-01-16 19:36:45

1

我解決這個問題,通過了把我的內容的視圖中的模態的VC嘗試一下本作放映視圖。然後將vc背景設置爲透明,並在viewWillAppear設置窗體背景透明。這意味着你只能看到內容。

// In Modal vc 
- (void)viewWillAppear:(BOOL)animated 
{ 
    self.view.superview.backgroundColor = [UIColor clearColor]; 
    [super viewWillAppear:animated]; 
} 

我從故事板中設置了所有內容,並使用了一個segue,如果使用代碼,您需要設置它。

parentViewController.modalPresentationStyle = UIModalPresentationPageSheet; 
2

我解決了它像@Stuart Campbell和@Viruss mca。

編輯

後@埃裏希的評論,我重寫了它在iOS的8運行了。下面是代碼:

HelpViewController * viewController = [[[HelpViewController alloc] init]]; 
    viewController.modalPresentationStyle=UIModalPresentationFormSheet; 
    [self presentViewController:viewController animated:YES completion:nil]; 

-

//HelpViewController.m 

- (void) viewWillLayoutSubviews{ 
    [super viewWillLayoutSubviews]; 
    if (!didLayout) { 
     [self layout]; 
     didLayout = YES; 
    } 
} 
- (void) layout{ 
    self.view.superview.backgroundColor = [UIColor clearColor]; 
    CGRect screen = self.view.superview.bounds; 
    CGRect frame = CGRectMake(0, 0, <width>, <height>); 
    float x = (screen.size.width - frame.size.width)*.5f; 
    float y = (screen.size.height - frame.size.height)*.5f; 
    frame = CGRectMake(x, y, frame.size.width, frame.size.height); 

    self.view.frame = frame; 
} 
+0

iOS中8 – Erich 2014-09-08 15:09:29

+0

不起作用創建定向問題變化 – 2015-02-19 12:37:58

+0

很好的解決方法! – iksnae 2017-06-14 22:33:30

61

對於iOS 8,用途:

self.preferredContentSize = CGSizeMake(width, height); 

我放在這viewDidLoad

斯威夫特3

self.preferredContentSize = CGSize(width: 100, height: 100) 
+2

在iOS 8 GM和AutoLayout打開的情況下效果很好。 thx – crosscode 2014-09-15 15:45:37

+0

在iOS 8和Xcode 6.0.1的發佈中很有用 – NMunro 2014-09-18 15:52:31

+1

如何在Interface Builder中設置? – Blip 2014-11-16 23:29:45

12

在屬性檢查器檢查Use Preferred Explicit Size

enter image description here

+2

未在肖像模式 – Beyaz 2016-09-27 14:40:34

+0

這僅適用於這是工作。 – 2017-05-28 18:44:29

+0

工作 – 2017-07-26 13:08:42

0

埃裏希的帖子與斯威夫特3爲我工作,但在iOS上10,我不得不使用:

self.preferredContentSize = CGSize(width, height) 

我也把它放在viewdidload

此外,就像avdyushin說,我不得不檢查Use Preferred Explicit Size框。

1

我的解決方案主要基於貼在這裏的其他解決方案,但因爲我不得不嘗試許多不同的事情來得到它的實際工作,我張貼。 Mine是針對iOS 10的,僅適用於縱向模式的Swift 3.1。(在橫向模式下未測試)我的解決方案的目標是顯示比呈現視圖更小的模式,以便我可以在後臺看到呈現視圖。

我不得不在Interface Builder中正確配置UIViewController,否則我的代碼將無法工作。這是我的設置。我發現我的解決方案可以使用Cross DissolveCover Vertical轉換樣式。我認爲我在IB的關鍵是演示文稿的Over Full Screen--它似乎沒有與此設置的其他值一起工作。

IB Settings

這裏是在的UIViewController我想模態呈現代碼。然後我在其他地方使用present(_:animated:completion:)。此外,在VC,我會介紹有模式,我有一個布爾值,didLayout初始化爲false

override func viewWillLayoutSubviews() { 
     super.viewWillLayoutSubviews() 

     if !didLayout { 

      let width:CGFloat = self.view.bounds.width * 0.95 //modify this constant to change the amount of the screen occupied by the modal 
      let height:CGFloat = width * 1.618 //golden ratio 

      self.view.superview!.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.15) //slightly dim the background to focus on the modal 
      let screen = self.view.superview!.bounds 
      let frame = CGRect(x: 0, y: 0, width: width, height: height) 
      let x = (screen.size.width - frame.size.width) * 0.5 
      let y = (screen.size.height - frame.size.height) * 0.5 
      let mainFrame = CGRect(x: x, y: y, width: frame.size.width, height: frame.size.height) 

      self.view.frame = mainFrame 

      didLayout = true 
     } 
    } 
1

設置preferredContentSize爲我做的,例如呈現EKEventEditViewController時無法正常工作在iOS上11。

它只有在我覆蓋preferredContentSize的獲取方時纔有效。是這樣的:

private class CLEventEditViewController: EKEventEditViewController { 
    override var preferredContentSize: CGSize { 
     get { 
      if let fullSize = self.presentingViewController?.view.bounds.size { 
       return CGSize(width: fullSize.width * 0.5, 
           height: fullSize.height * 0.75) 
      } 
      return super.preferredContentSize 
     } 
     set { 
      super.preferredContentSize = newValue 
     } 
    } 
} 

enter image description here enter image description here