2014-10-27 122 views
9

我試圖在iOS 7和iOS 8上展示帶有透明背景的viewcontroller。 只需將viewcontroller的modalPresentationStyle屬性更改爲FormSheet,就可以使其在iOS 7.1上工作。作爲具有透明背景的模式呈現UIViewController

我想要的是上ios7一個普遍的方式+

我一直在使用其他選項modalPresentationStyle,喜歡嘗試:OverCurrentContext,CurrentContext和PageSheet。

我也嘗試使用modalPresentationStyle.Custom,但沒有任何成功。

我有NavigationController,如果這有助於任何事情。

用於呈現視圖控制器的代碼:所呈現的ViewController的

InfoViewController *info = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil]; 
[self presentViewController:info animated:YES completion:nil]; 

而對於viewDidLoad中(我認爲有這樣的相關部分)中的代碼:

​​

I' m使用swift和Xcode 6. 這裏是我現在和我想要的東西的屏幕截圖:

enter image description hereenter image description here

下面是一個例子代碼:https://github.com/pbassut/TransBackgroundViewController

+0

你可以發佈到github所以我們可以玩的代碼? – 2014-10-28 00:56:17

+0

你的意思是整個代碼? – 2014-10-28 02:47:02

+0

只是沒有工作的相關部分,所以我們可以將其粘貼到新項目上並查看失敗並進行更正。 – 2014-10-28 02:48:14

回答

16

對於這個問題,那些仍然呈現的UIViewController設置提出的UIViewController的modalPresentationStyle到.Custom,它會在iOS 8(6.1的Xcode)工作之前。也就是說,你應該把它在呈現的UIViewController

10

我試過這個解決方案,它適用於iOS的兩個7,8:

if (UIDevice.currentDevice().systemVersion.integerValue >= 8) 
{ 
    //For iOS 8 
    presentingViewController.providesPresentationContextTransitionStyle = true; 
    presentingViewController.definesPresentationContext = true; 
    presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; 
} 
else 
{ 
    //For iOS 7 
    presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext; 
} 

注:注意之間的差異'presentsViewController'和'presentedViewController'。

0

我有一個類似的問題,我想要做類似你這裏介紹的第二個屏幕的東西(巴西/葡萄牙報紙!)

我解決了它這樣的。我插入了一個覆蓋整個視圖控制器的UIView,然後我去了Attributes Inspector,並將UIView的背景顏色設置爲黑色,具有50%的不透明度。然後,我插入上面的第一個彼此的UIView,以及i製作一個

5

我能夠沒有代碼來實現這一點:

  1. 在情節串連圖板,呈現視圖控制器上(即前你繼續想要變得透明),轉到屬性檢查器。在該選項卡上,有「定義上下文」和「提供上下文」複選框。檢查這些。

  2. 在segue上,將其設置爲「Present Modally」。

  3. 在目標/呈現的視圖控制器上,轉到屬性選項卡。在「Presentation」下拉菜單中,選擇「Over Current Context」。

  4. 在目標視圖控制器上,將其視圖設置爲具有清晰的背景。

  5. 在目標視圖控制器上,放下2個UIViews:一個是「透明」的,另一個是「內容」。將透明設置爲擁有你喜歡的任何字母,並將你的所有垃圾放入「內容」中。

+0

就我而言,這是正確的答案。 – 2016-12-31 07:20:13

+0

這個,你甚至不需要額外的視圖,只需設置視圖背景顏色不透明度,如果你想要一些透明度 – SimonBarker 2017-06-27 10:39:49

3

我通過以下步驟實現Swift 2.0中各種樣式的透明。分析它以與您的代碼兼容。

透明視圖控制器(模式視圖控制器)是由一個按鈕點擊主視圖控制器時由segue啓動的。

Segue公司設置:

enter image description here

在主視圖控制器:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{ 
let vc = segue.destinationViewController as! ModalViewController 
vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext //All objects and view are transparent 
} 

模態視圖控制器上故事板:

  1. 如果需要視圖和所有的對象都透明

enter image description here

  • 如果需要視圖僅透明和在對象不是透明
  • enter image description here

    對你的Mo DAL視圖控制器

    override func viewDidLoad() { 
    super.viewDidLoad() 
    
    view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8) //view only black coloured transparent 
    
    } 
    

    樣本屏幕截圖:

    1. 查看及對象是透明

    enter image description here

  • 查看透明
  • enter image description here

    如有其它疑問,請在這裏評論:)

    如果混淆或更多與透明視圖控制器然後watch my video tutorial您的YouTube:d

    2

    iOS8上+

    下面的代碼片段將解決iOS8中的這個問題+

    SecondViewController *secondViewController = [[SecondViewController alloc] init]; 
    secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;   
    [self presentViewController:secondViewController animated:YES completion:nil];  
    
    2

    以上都不適合我。爲了在iOS 10上實現這個功能,我只需要: presented.modalPresentationStyle = .overFullScreen presenting.present(presented, animated: true) 然後將呈現的視圖控制器的根視圖設置爲透明或半透明顏色。不要更改其alpha或所有的子視圖將具有相同的alpha。