2017-08-21 31 views
0

我不知道爲什麼,但當鍵盤出現時,它將我的細胞推出屏幕。我正在收集視圖中工作,以編程方式呈現我的視圖。這是我得到的錯誤,當發生這種情況:鍵盤將我的細胞推出屏幕

的UICollectionViewFlowLayout的行爲沒有定義的,因爲: 2017年8月21日13:09:19.710 audibleAPP [19038:1975381]的項目高度必須小於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 
} 

MainController

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

的解決方案是設置automaticallyAdjustsScrollViewInsets爲False在UICollectionViewController子類的某處,諸如在viewDidLoad中:

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() 
    } 
} 

希望這將幫助你!!!!