2016-02-18 6 views
11

私はiosアプリケーションを開発しています。私はメインビューを持っており、このビューでは ぼんやりした背景(不透明な黒色)のモーダルビューコントローラを表示しようとしています。 問題は、ステータスバーがこの色の影響を受けず、同じままであることです。モーダルビューコントローラはステータスバーをカバーしません

これは私がビューコントローラを提示する方法である:ここでは

let shareViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ShareViewController") as! ShareViewController 
      shareViewController.battle = battle 
      shareViewController.delegate = self 
      let animation = CATransition() 
      animation.duration = 1 
      animation.type = kCATransitionFade 
      self.view.window?.layer.addAnimation(animation, forKey: kCATransition) 
      presentViewController(shareViewController, animated: false) { 
       () in 
       // nothing here 
      } 

は、問題を示すためにいくつかのスクリーンショットです:

これが問題(ステータスバーの色)である: Problem illustration これがモーダルストーリーボードのビュー: storyboard

+0

ヘルプユーザー –

+0

ShareViewControllerのコードを共有してください。もしあれば –

答えて

0

このコードは、私がUIViewControllerをアルファ= 1で提示しているときに私にとって役に立ちます。本UIViewController様:

let storyBoard = UIStoryboard(name: "Main", bundle: nil) 
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "AddComment") as! AddCommentViewController 
destinationVC.modalPresentationStyle = .overCurrentContext //this line is important 
destinationVC.delegate = self 
destinationVC.restId = self.restaurant.id 
self.present(destinationVC, animated: true, completion: nil) 

その後にdestinationVCビューコントローラ

override func viewWillDisappear(_: Bool) { 
     UIView.animate(withDuration: 1, animations: {() in 
      self.view.backgroundColor = .clear 
     }) 
     super.viewWillDisappear(true) 
    } 

override func viewWillAppear(_: Bool) { 
    UIView.animate(withDuration: 1, animations: {() in 
     self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) 
    }) 
    super.viewWillAppear(true) 
} 

そのbackgroundColor.clearからviewDidLoad又はストーリーボードを設定します。したがって、UIViewControllerはステータスバーを含む全画面をカバーします。

0

あなたは非常に実用的であるとあなたのモーダルビューコントローラがアップすると、単純にステータスバーを隠すことができます:ここで

override func prefersStatusBarHidden() -> Bool { 
    return true 
} 
+0

私はこれを、* OPの探しているものではなく、ステータスバーを隠すことではなく、それを覆っていると仮定します。とにかく、回避策かもしれません... –

+0

AppleのSDKは純粋で美しいものとはほど遠いので、純粋で美しい解決策を求める価値がないということがわかりました。 – ClayJ

+0

実際には、あなたは限界を超えて見なければなりません)) –

0

は、あなたが探しているかもしれないソリューションです。

if let window = UIApplication.shared.keyWindow { 
    window.windowLevel = UIWindowLevelStatusBar + 1 
} 

主なアイデアこのコードの背後には、アプリケーションのwindowにステータスバーのウィンドウレベルより低いウィンドウレベルがあります。そして、このコードがしていることは、あなたのウィンドウのウィンドウレベルをステータスバーウィンドウレベルより高くして、ウィンドウがステータスバーをカバーできるようにするだけです。このコードは、ビューコントローラを表示する直前にメインスレッドで呼び出される必要があることを忘れないでください。がんばろう!

+0

これはかなり危険です。 'keyWindow'のレベルを変更することで、キーボードを表示するときに別のウィンドウに表示される非常に奇妙な効果を得ることができます。また、解雇時に元のレベルに戻る必要があります。ステータスバーと同じサイズの新しいウィンドウを作成し、それをステータスバーの上に置き、コントローラが表示され消えたときに隠して表示するというアドバイスがあります。 – Sulthan

+0

危険なものは公開されていません。私はちょうどOPが尋ねているように効果を生成するソリューションを投稿しています。もちろん、あなたの考えは正しいです、注意して使用されない場合、この方法は、いくつかの望ましくない出力を生成する可能性がありますが、私はキーボードについてあなたの意見を主張することができます。とにかく、違反はありません、あなたのソリューションはこれよりも見栄えが良いです。私はあなたのソリューションを使用するOPをお勧めします。 –

0

カスタムアニメーションのトランジションは、UIViewControllerAnimatedTransitioningを使用して実行する必要があります。あなたが望むすべてはあなたが表示しようとしているのViewControllerのmodalTransitionStyleプロパティを変更することにより、それを持つことができますフェードアニメーションがある場合 https://www.raywenderlich.com/110536/custom-uiviewcontroller-transitions

:ここでは、この目的のためのチュートリアルです。あなたのコードをこのように固定することにより、

てみ:

guard let shareViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ShareViewController") as! ShareViewController else { 
    //Fallback in case of nil? 
    return 
} 
shareViewController.modalTransitionStyle = .crossDissolve 
presentViewController(shareViewController, animated: true, completion: nil) 

presentViewController(shareViewController, animated: true, completion: nil)は、私は、次のコードの作品を​​自分の問題を再現することはできませんpresent(shareViewController, animated: true, completion: nil)

5

だろうSWIFT 2.同等の迅速な3用であることに注意してください私の単一のビューのアプリで問題なしで:

let viewController = UIViewController() 
viewController.modalPresentationStyle = .overFullScreen 
viewController.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) 

let animation = CATransition() 
animation.duration = 1 
animation.type = kCATransitionFade 
self.view.window?.layer.add(animation, forKey: kCATransition) 

self.present(viewController, animated: false, completion: nil) 

ただし、あなたはビュー。あなたの内部のコントローラから提示するとき時々、あなたは奇妙な効果を得ることができます:

self.view.window?.rootViewController?.present(viewController, animated: false, completion: nil) 

をまた、あなたが正しいmodalPresentationStyleを使用していることを確認してください。

Example

0

あなたはスウィフト3用のコントローラを表示するには、このコードを追加することができます

let statusView: UIView = UIView(frame: CGRect(x: 0.0, y: -20.0, width: UIScreen.main.bounds.size.width, height: 20.0)) 
statusView.backgroundColor = UIColor.black 
statusView.alpha = 0.8 
self.addSubview(self.statusView) 
0

UIWindowLevelAlertにウィンドウを提示し、その後、UIWindowのルートビューコントローラとしてあなたのビューコントローラを設定します。レベル。

以下はSwift 3のクラスで、ステータスバーを含む他のすべてのUI要素よりもモーダルポップアップをアニメートするために使用されます。スクリム・ビューを使用して背景UIをシェードし、タッチを傍受してビューを閉じる。

import UIKit 

class ModalViewController: UIViewController { 

    private let scrimView: UIView = { 
     let view = UIView() 
     view.translatesAutoresizingMaskIntoConstraints = false 
     view.backgroundColor = UIColor.black 
     view.alpha = 0.0 
     return view 
    }() 

    private var myWindow: UIWindow? 

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

     // Setup scrim View 
     view.addSubview(scrimView) 
     view.topAnchor.constraint(equalTo: scrimView.topAnchor).isActive = true 
     view.leftAnchor.constraint(equalTo: scrimView.leftAnchor).isActive = true 
     view.rightAnchor.constraint(equalTo: scrimView.rightAnchor).isActive = true 
     view.bottomAnchor.constraint(equalTo: scrimView.bottomAnchor).isActive = true 

     let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismiss as (Void) -> Void)) 
     scrimView.addGestureRecognizer(tapGestureRecognizer) 

     // Layout custom popups or action sheets 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     UIView.animate(withDuration: 0.25) { 
      self.scrimView.alpha = 0.5 
      // Animate in custom popups or action sheets 
     } 
    } 

    func present() { 
     myWindow = UIWindow(frame: UIScreen.main.bounds) 
     myWindow?.windowLevel = UIWindowLevelAlert 
     myWindow?.backgroundColor = UIColor.clear 
     myWindow?.rootViewController = self 
     myWindow?.isHidden = false 
    } 

    func dismiss() { 

     UIView.animate(
      withDuration: 0.25, 
      animations: { 
       self.scrimView.alpha = 0.0 
       // Animate out custom popups or action sheets 
      }, 
      completion: { success in 
       self.myWindow = nil 
      } 
     ) 
    } 
} 

ビュー提示する:スクリム上の任意の場所をタップし、ビューを閉じるには

let modalView = ModalViewController() 
modalView.present() 

を。

関連する問題