2017-02-08 13 views
3

UIViewは、下からviewControllerの1/3をカバーするUIViewControllerしか持っていません。この半画面上にViewControllerを表示する方法

enter image description here

と同じように、私は他のViewControllerにこののViewControllerを提示したいです。下からアニメーション表示され、下にアニメーション表示されます。

しかし、私はそれが画面全体をカバーすることは望ましくありません。提示されたviewControllerは、背面に表示する必要があります。

基本的な質問のようですが、私はそれを行うことができません。誰かが私をその方向に向けることができますか?

編集:

これは私がこれまでにしようとしているものです。私は、これらのクラス

// MARK: - 

class MyFadeInFadeOutTransitioning: NSObject, UIViewControllerTransitioningDelegate { 
var backgroundColorAlpha: CGFloat = 0.5 
var shoulDismiss = false 

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    let fadeInPresentAnimationController = MyFadeInPresentAnimationController() 
     fadeInPresentAnimationController.backgroundColorAlpha = backgroundColorAlpha 

    return fadeInPresentAnimationController 
} 

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    let fadeOutDismissAnimationController = MyFadeOutDismissAnimationController() 

    return fadeOutDismissAnimationController 
} 

} 

// MARK: - 

class MYFadeInPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning { 

let kPresentationDuration = 0.5 
var backgroundColorAlpha: CGFloat? 

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
    return kPresentationDuration 
} 

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
    let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)! 

    toViewController.view.backgroundColor = UIColor.clear 

    let toViewFrame = transitionContext.finalFrame(for: toViewController) 
    let containerView = transitionContext.containerView 

    if let pickerContainerView = toViewController.view.viewWithTag(kContainerViewTag) { 
     let transform = CGAffineTransform(translationX: 0.0, y: pickerContainerView.frame.size.height) 
     pickerContainerView.transform = transform 
    } 

    toViewController.view.frame = toViewFrame 
    containerView.addSubview(toViewController.view) 

    UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveLinear , animations: { 
     toViewController.view.backgroundColor = UIColor(white: 0.0, alpha: self.backgroundColorAlpha!) 

     if let pickerContainerView = toViewController.view.viewWithTag(kContainerViewTag) { 
      pickerContainerView.transform = CGAffineTransform.identity 
     } 

    }) { (finished) in 
     transitionContext.completeTransition(true) 
    } 
} 

} 

// MARK: - 

class MYFadeOutDismissAnimationController: NSObject, UIViewControllerAnimatedTransitioning { 
let kDismissalDuration = 0.15 

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
    return kDismissalDuration 
} 

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)! 
    let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)! 
    let containerView = transitionContext.containerView 

    containerView.addSubview(toViewController.view) 
    containerView.sendSubview(toBack: toViewController.view) 

    UIView.animate(withDuration: kDismissalDuration, delay: 0.0, options: .curveLinear, animations: { 
     //   fromViewController.view.backgroundColor = UIColor.clearColor() 
     //   if let pickerContainerView = toViewController.view.viewWithTag(kContainerViewTag) { 
     //    let transform = CGAffineTransformMakeTranslation(0.0, pickerContainerView.frame.size.height) 
     //    pickerContainerView.transform = transform 
     //   } 
     fromViewController.view.alpha = 0.0 

    }) { (finished) in 
     let canceled: Bool = transitionContext.transitionWasCancelled 
     transitionContext.completeTransition(true) 

     if !canceled { 
      UIApplication.shared.keyWindow?.addSubview(toViewController.view) 
     } 
    } 
} 

} 

を作成し、提示されているのViewControllerに、私はそれが現在のViewControllerを行う

var customTransitioningDelegate: MYFadeInFadeOutTransitioning? = MYFadeInFadeOutTransitioning() 

    init() { 
    super.init(nibName: "SomeNibName", bundle: Bundle.main) 
    transitioningDelegate = customTransitioningDelegate 
    modalPresentationStyle = .custom 

    customTransitioningDelegate?.backgroundColorAlpha = 0.0 
} 

を次のようにやっていると私は同様に、バックグラウンドのViewControllerを見ることができます。しかし、私はそれがアニメーションの下から提示されたい。そして、アニメーションで一番下に降りてください。どうやってやるの ?

+0

あなたはウルしようとしたコード –

+0

を表示することができますAnbu.Karthik @子 –

+0

としてビューコントローラを追加することができます今すぐ確認してください。 –

答えて

5

コンテナビューを使用してこの機能を実装することをおすすめします。参照のためにhereを見てください。

これは、別のViewController内のUIViewに埋め込まれたUIViewController(およびそのサブクラス)を表示できることを意味します。その後、フェードインや任意のものをアニメートできます。

+0

コンテナビューのコンセプトを使用する必要があります。 –

0

また、ビューコントローラーをモーダルに表示し、プレゼンテーションスタイルプロパティをフルスクリーン、トランジションスタイルプロパティで垂直方向に、ビューの背景色のアルファ成分を0.1に設定することで、効果を得ることができます。

2

これを達成するにはUIPresentationControllerを使用できます。 ViewControllerを提示の上UIViewControllerTransitioningDelegateメソッドを実装し、あなたが同様の要件があり、このanswerを参照することができ

func presentationController(forPresented presented: UIViewController, 
          presenting: UIViewController?, 
           source: UIViewController) -> UIPresentationController? 

デリゲートメソッドからあなたPresentationControllerを返します。代わりに、他の回答に示唆されているように、UIViewアニメーションや埋め込みビューコントローラを使用することもできます。

編集:あなたはそれはあなたがフレームを設定することが可能になり、私はUIPresentationControllerクラスを使用することをお勧め半分画面の上にビューコントローラを提示したい場合はGithubの

https://github.com/martinnormark/HalfModalPresentationController

8

で見つかった

サンプルプロジェクト表示コントローラが表示されているときに表示されます。あなたがpresentedViewControllerを却下するまで、このメソッドはpresentingViewControllerのユーザインタラクションを停止するので、ユーザがpresentingViewControllerと対話しながら画面の半分以上を表示するには、他の回答と同様にコンテナビューを使用する必要があります提案する。 これは、あなたが

たくないものをUIPresentationControllerクラスの例
import UIKit 
class ForgotPasswordPresentationController: UIPresentationController{ 
    let blurEffectView: UIVisualEffectView! 
    var tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer() 
    func dismiss(){ 
     self.presentedViewController.dismiss(animated: true, completion: nil) 
    } 
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) { 
     let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark) 
     blurEffectView = UIVisualEffectView(effect: blurEffect) 
     super.init(presentedViewController: presentedViewController, presenting: presentingViewController) 
     tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismiss)) 
     blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
     self.blurEffectView.isUserInteractionEnabled = true 
     self.blurEffectView.addGestureRecognizer(tapGestureRecognizer) 
    } 
    override var frameOfPresentedViewInContainerView: CGRect{ 
     return CGRect(origin: CGPoint(x: 0, y: self.containerView!.frame.height/2), size: CGSize(width: self.containerView!.frame.width, height: self.containerView!.frame.height/2)) 
    } 
    override func dismissalTransitionWillBegin() { 
     self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in 
      self.blurEffectView.alpha = 0 
     }, completion: { (UIViewControllerTransitionCoordinatorContext) in 
      self.blurEffectView.removeFromSuperview() 
     }) 
    } 
    override func presentationTransitionWillBegin() { 
     self.blurEffectView.alpha = 0 
     self.containerView?.addSubview(blurEffectView) 
     self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in 
      self.blurEffectView.alpha = 1 
     }, completion: { (UIViewControllerTransitionCoordinatorContext) in 

     }) 
    } 
    override func containerViewWillLayoutSubviews() { 
     super.containerViewWillLayoutSubviews() 
     presentedView!.layer.masksToBounds = true 
     presentedView!.layer.cornerRadius = 10 
    } 
    override func containerViewDidLayoutSubviews() { 
     super.containerViewDidLayoutSubviews() 
     self.presentedView?.frame = frameOfPresentedViewInContainerView 
     blurEffectView.frame = containerView!.bounds 
    } 
} 

でこれもぼかしビューとあなたがpresentedViewControllerフレームの外にタップしたとき却下するタップを追加します。あなたはpresentedViewControllertransitioningDelegateを設定し、そこに

presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?

メソッドを実装する必要があります。また、私はUIPresentationControllerの使用量が非常にクリーンなアプローチであることがわかりpresentedViewController

modalPresentationStyle = .custom 設定することを忘れないでください。幸運

+0

これはすばらしい答えです。表示されたビューコントローラがすでに「UIPopoverPresentationController」を持っているときに、どのようにそれを適応させることができますか?iPhone上では、提示されたビューコントローラを画面の半分にしか表示しません。 – Jan

1

この機能を達成するための更新されたコードがあります。あなたが選択subViewControllerぼかし効果のためにそれにUIViewsを追加ストーリーボードするのViewController

@IBAction func btnShow(_ sender: Any) { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let pvc = storyboard.instantiateViewController(withIdentifier: "SubViewController") as! SubViewController 
     pvc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext 

     self.present(pvc, animated: true, completion: nil) 
    } 

ゴーを提示するアクションでトップ(にその制約を設定:0を、下:0、大手: 0、末尾:0)をすべての側に変更し、その色を黒色に変更し、アルファベットに設定します。そして、そのオプションのために他のUIViewsを追加するには、にその制約を設定した後(トップ: - ボトム:0、大手:0、末尾:0)は、それがスーパーで同じ高さに高さ制約(自己のset .View)を変更し、の乗数を0.33または0.34に変更します。 希望します。 スクリーンショート。 enter image description here

0

あなたはOver Full Screenとして設定されたプレゼンテーションをし、クリアするために提示ビューコントローラのbackgroundColorを設定することにより、Present Modallyストーリーボードセグエを使用することができます。

Present modally

関連する問題