2017-08-21 27 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() 
    } 
} 

希望这将帮助你!!!!