2016-06-22 17 views
0

私は写真閲覧アプリを作成しています。ユーザーは閲覧中の写真の詳細やコメントを閲覧できます。これを行うには、詳細とコメントの間の切り替えと、詳細とコメントを提供する2つのUITableViewControllersとして動作する、分割されたコントロールを持つUIViewController(parentVC)があります。 Storyboard of the view controllers in questionサブビューとして表示が追加されたときにContentInsetが更新されない

ビューを切り替えるには、tableVCがストーリーボードからインスタンス化され、parentVCとしてchildViewControllerとして追加され、ビューがサブビューとして追加されます。

segmentControlはnavigationControllerのnavBarに配置されているので、私はparentVCのナビゲーションバーでniceを再生したいと考えています。最初のtableview(detailVC)がインスタンス化されると、そのcontentInsetが自動的に調整されます。

ただし、2番目のtableview(commentsVC)がセグメント化されたコントロールからの切り替え後にインスタンス化されると、contentInsetは設定されず(実際にはすべてのエッジで0になります)、tableviewはnavBarの背後に隠されます。しかし、デバイスが回転すると、テーブルビューはそのインセットを調整し、再び正常に動作します。

私は、どのchildVCが最初に存在するかを切り替えると、最初に提示されたVCのcontentInsetが常に設定されるため、この問題はiOSによってautomaticadjustsscrollviewinsetsがどのように行われるかと関係していると思われます。追加されたときに2番目のVCのcontentInsetを設定する方法があるかどうかはわかりません。

私は両方のchildVCのcontentInsetをparentVCのnavigationBarに合わせて設定することはできますか? UIEdgeInsetを作成することは回避策ですが、デバイスの向きが変更されたときには更新されません。もっと小さな問題は、childVCがプログラムによってインスタンス化され、ストーリーボード自体にリンクされているかのように動作するように、ナビゲーションコントローラのレイアウトとインセットでどう作業すればよいかです。

次は私のコードで、parentVCからchildVCをインスタンス化するときのものです。

if index == 0 { //details 

     if detailsVC == nil { 
      detailsVC = storyboard?.instantiateViewControllerWithIdentifier("InfoPaneDetailsVC") as! InfoPaneDetailsVC 
     } 
     if commentsVC != nil { 
      commentsVC.view.removeFromSuperview() 
      commentsVC.removeFromParentViewController() 
     } 

     addChildViewController(detailsVC) 
     detailsVC.didMoveToParentViewController(self) 
     view.addSubview(detailsVC.view) 
     view.layoutSubviews() 

    } else if index == 1 { //comments 

     if commentsVC == nil { 
      commentsVC = storyboard?.instantiateViewControllerWithIdentifier("InfoPaneCommentsVC") as! InfoPaneCommentsVC 
     } 
     if detailsVC != nil { 
      detailsVC.view.removeFromSuperview() 
      detailsVC.removeFromParentViewController() 
     } 

     addChildViewController(commentsVC) 
     commentsVC.didMoveToParentViewController(self) 
     view.addSubview(commentsVC.view) 
     view.layoutSubviews() 

    } 

答えて

0

テーブルビューは、ナビゲーションバーの後ろに隠れています。しかし、デバイスが回転すると、テーブルビューはそのインセットを調整し、 は再び正常に動作します。

私はあなたが見ているものを複製することができます。私はなぜデバイスを回転させてアンダーラップ*を取り除くのか分からない。私はアプリを横向きで起動することもでき、TableViewはNavigationBarをアンダーラップさせます。次に、縦に回転するとアンダーラップが消えます。

ソリューションのカップルが私のために働く:

  1. edgesForExtendedLayout = .None
  2. navigationController?.navigationBar.translucent = false

のViewControllerの親がNavigationController(またはTabBarController)であれば、2つのプロパティがどのようにアンダーラップの作品を決定:

  1. ViewControllerのedgesForExtendedLayoutプロパティ。
  2. NavigationBarのtranslucentプロパティ。

edgesForExtendedLayoutプロパティは、のViewControllerのビューがすべて半透明のバーをアンダーラップすることを意味します.Allのデフォルト値を持っています。

あなたのNavigationBarが、このような半透明であるかどうかを確認することができます。

import UIKit 

class InfoViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if navigationController?.navigationBar.translucent == true { 
      print("NavigationBar is translucent.") 
     } 

     if edgesForExtendedLayout == .All { 
      print("This ViewController's view will underlap a translucent NavigationBar.") 
     } 

私が取得:

NavigationBar is translucent. 
This ViewController's view will underlap a translucent NavigationBar. 

UINavigationBar docsナビゲーションバー、さらに**

を言いますデフォルトでは半透明です。 の半透明をオフまたはオンにしても、背景には のバックグラウンドがないため、ボタンには影響しません。

あなたはこのようにアンダーラップを防ぐことができます。

import UIKit 

class InfoViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     edgesForExtendedLayout = .None 
    } 

.None任意の半透明のバーをアンダーラップしないのViewControllerの見解を伝えます。また、.Bottomを使用することもできます。つまり、ビューには半透明の下端バー、つまり半透明の上端バーのみが表示されます。 (edgesForExtendedLayoutに割り当てることができるさまざまな値(または値の組み合わせ)を確認するには、docsをチェックしてください)。

それとも、あなたはこのようにアンダーラップを防ぐことができます。

import UIKit 

class InfoViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     navigationController?.navigationBar.translucent = false 
    } 

edgesForExtendedLayoutの値のみを半透明のバーに適用されます - ビューが不透明なバーを背後に重ねることはありません。あなたのコードで半透明をfalseに設定しなければならない場合は、ストーリーボードでナビゲーションバーを選択し、属性インスペクタで半透明をオフにします。デバイスを回転させた後


*、私はedgesForExtendedLayoutをプリントアウトし、ナビゲーションバーのtranslucentプロパティと、彼らは変更されませんでした:

class InfoViewController: UIViewController { 

    override func viewWillTransitionToSize(size: CGSize, 
     withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) 
    { 
     super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) 

     //executes before transition 

     coordinator.animateAlongsideTransition(//unlabeled first argument: 
      { (UIViewControllerTransitionCoordinatorContext) -> Void in 
       //executes during transition 
      }, 
      completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in 
       //executes after transition 

       print("After rotating:") 
       if self.navigationController?.navigationBar.translucent == true { 
        print("NavigationBar is translucent.") 
       } 

       if self.edgesForExtendedLayout == .All { 
        print("This ViewController's view will underlap a translucent NavigationBar.") 
       } 

      } 
     ) 
    } 

--output:-- 
NavigationBar is translucent. 
This ViewController's view will underlap a translucent NavigationBar. 
After rotating: 
NavigationBar is translucent. 
This ViewController's view will underlap a translucent NavigationBar. 

**をNavigationBarは、背景画像、NavigationBarの値を持つ場合半透明のプロパティは画像のアルファに応じて真または偽に設定されます。 UINavigationBar docsTranslucencyセクションを参照してください。

関連する問題