2017-08-21 8 views
0

なぜキーボードが表示されたら、画面からセルを押し出すのか分かりません。私はコレクションビューで作業しています。私のビューはプログラム的に配置されています。このことが起こるとき、私は取得エラー:キーボードが画面から自分のセルを押し出す

のでUICollectionViewFlowLayoutの振る舞いが定義されていない:アイテムの高さがより小さくなければなりません。[1975381 19038] 2017年8月21日13:09 19.710 audibleAPP UICollectionViewの高さから、セクションの上端と下端の値を引いたものから、コンテンツの上端と下端の値を差し引いた値。

そして、この私が持っているものコード

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    window = UIWindow(frame: UIScreen.main.bounds) 
    window?.makeKeyAndVisible() 

    let layout: UICollectionViewFlowLayout = { 
     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = .horizontal 
     return layout 
    }() 

    let mainView = MainVC(collectionViewLayout: layout) 
    window?.rootViewController = mainView 

    return true 
} 

メインコントローラ

override func viewDidLoad() { 
    super.viewDidLoad() 

    collectionView?.backgroundColor = .blue 
    collectionView?.delegate = self 
    collectionView?.dataSource = self 
    collectionView?.isPagingEnabled = true 

    collectionView?.register(PageCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
    collectionView?.register(LoginCell.self, forCellWithReuseIdentifier: loginIdentifier) 

多すぎるので、私はすべてのコードを入れていません、私はどこに誤りがあると思うかを入れようとしました。

Here is a picture of the simulator, the red view is all the cell:

答えて

0

ソリューションは、のviewDidLoadのように、どこかUICollectionViewControllerサブクラスでFalseにautomaticallyAdjustsScrollViewInsetsを設定することです:

self.automaticallyAdjustsScrollViewInsets = False 
+0

私はすでにそれを試して、うまくいきませんでした。私はviewDidLoad内に入れます –

+0

estimatedItemSizeを設定しようとします –

+0

どちらも動作しません、私は本当に何ができるか分からない。 –

1

ステップ1:

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    let notificationCenter = NotificationCenter.default 
    notificationCenter.addObserver(self, selector: #selector(self.adjustForKeyboard(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) 
} 

ステップ2:

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 
    let notificationCenter = NotificationCenter.default 
    notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) 
} 

ステップ3:これはあなたを助ける

//keyboard with tableview/ collection view handling 
func adjustForKeyboard(notification: Notification) { 
    if let userInfo = notification.userInfo, let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { 

     if (endFrame.origin.y) >= UIScreen.main.bounds.size.height { 
      //self.keyboardHeightLayoutConstraint?.constant = 0.0 
      let contentInsets: UIEdgeInsets = .zero 
      self.collectionView.contentInset = contentInsets 
      self.collectionView.scrollIndicatorInsets = contentInsets 

     } else { 
      let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, endFrame.size.height+05, 0.0); //05 is padding between your textfield and keypad. 
      self.collectionView.contentInset = contentInsets 
      self.collectionView.scrollIndicatorInsets = contentInsets 
     } 
     self.collectionView.updateConstraints() 
     self.collectionView.layoutIfNeeded() 
    } 
} 

・ホープ!!!!

関連する問題