簡體   English   中英

presentViewController黑色背景而不是透明

[英]presentViewController black background instead of transparent

我希望以標准方式向用戶呈現(從屏幕底部向上滑動)。 大約一半的視圖是透明背景,下半部分有一些輸入字段(想象鍵盤彈出的方式)。 當我在rootViewController上調用[self presentViewController]時,它會向上滑動視圖,但是大約半秒后,視圖過去是透明的,而是用黑色代替。 這與presentViewController和presentModalViewController都會發生。 如何改變這種行為?

這是可能的,rockybalboa在raywenderlich.com上的論壇帖子中提供了解決此問題的方法

iOS 8 UIModalPresentationCurrentContext不透明?

該解決方案在Objective-C中引用巴爾博亞的答案:

在iOS 8之前,您執行此操作:

[backgroundViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
[backgroundViewController presentViewController:_myMoreAppsViewController animated:NO completion:nil];

在iOS 8中,您必須這樣做:

backgroundViewController.providesPresentationContextTransitionStyle = YES;
backgroundController.definesPresentationContext = YES;
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

用Swift等價物補充上面的代碼:

backgroundViewController.providesPresentationContextTransitionStyle = true
backgroundController.definesPresentationContext = true
overlayViewController.modalPresentationStyle = .OverCurrentContext

在iOS 8.1和Swift 1.2上使用Xcode 6.3 beta 3實現了這一點,它完美地運行。

只是評論我在查找和嘗試Ray Wenderlich論壇解決方案之前, 已經查看了三個不同的SO問題 - 現在標記為重復

據我所知,當您呈現模型視圖控制器時,不支持透明背景。 嘗試將控制器保留在根視圖控制器中,只需將子視圖添加到根視圖控制器即可。

使用SWIFT 4,只需將此代碼添加到您想要具有透明背景的視圖控制器即可。

override func viewDidLoad()
{
    super.viewDidLoad()
    self.modalPresentationStyle = .overFullScreen
    self.view.backgroundColor = UIColor.clear

}

最后,看起來它不可能是透明的,我通過將這個視圖添加為根視圖控制器邊界之外的子視圖,然后使用動畫塊將其滑動到位來解決這個問題。 不是很多額外的代碼,但能夠使用標准的iOS行為來做這件事會很不錯。

在創建控制器時,我在短暫的延遲后出現了類似的黑色背景問題

    Disclaimer *vc = [[Disclaimer alloc]init];

解決這個問題的方法是在IB中創建一個相應的對象,並使用它的storyboard ID實例化viewcontroller:

     Disclaimer *vc = (Disclaimer *)[self.storyboard instantiateViewControllerWithIdentifier:@"SBIDDisclaimer"];
[self presentViewController:vc animated:YES completion:nil];

我想通過IB進行一些額外的初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM