【问题标题】:Keyboard push my cell out of the screen键盘将我的手机推出屏幕
【发布时间】:2017-08-21 17:16:25
【问题描述】:

我不知道为什么,但是当键盘出现时,它会将我的单元格推出屏幕。我正在收集视图中工作,以编程方式放置我的视图。这是发生这种情况时我得到的错误:

未定义 UICollectionViewFlowLayout 的行为,因为: 2017-08-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
}

主控制器

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:

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案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()
        }
    }
    

    希望对你有帮助!!!!

    【讨论】:

      【解决方案2】:

      解决方案是在你的 UICollectionViewController 子类中的某处自动将AdjustsScrollViewInsets 设置为 False,例如在 viewDidLoad 中:

          self.automaticallyAdjustsScrollViewInsets = False
      

      【讨论】:

      • 我已经尝试过了,但没有成功。我把它放在 viewDidLoad
      • 尝试设置estimatedItemSize
      • 也不起作用,我真的不知道会是什么。
      【解决方案3】:

      如果其他解决方案不适合您。我在 Swift 4 下有一个新方法。我在另一个 StackOverFlow 线程上遇到的解决方案,尽管它有一次获得 0 票。

      我不会再次重新发布全部费用,但如果您按照我在该线程中的回答,您可以找到一个不受键盘影响的简单 UICollectionView 示例。主要前提是将 UICollectionView 作为 ViewController 的子视图,该子视图将成为 Root View Controller。

      UICollectionView - When the keyboard appears the entire collection view is shifted with the keyboard

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-24
        • 2023-03-26
        • 1970-01-01
        • 2019-01-12
        • 2011-05-06
        • 1970-01-01
        • 2013-09-10
        • 1970-01-01
        相关资源
        最近更新 更多