gpt4 book ai didi

ios - UICollectionView - 当键盘出现时,整个 Collection View 随键盘移动

转载 作者:行者123 更新时间:2023-11-28 23:51:31 24 4
gpt4 key购买 nike

这不是我想要的效果。仅当 Collection View 设置为水平流布局时才会发生这种情况。我看过其他一些关于这个问题的帖子,但所提供的答案都不起作用。有人找到解决办法了吗?

我提供了两个屏幕截图,显示键盘触发之前和之后的 UITextField。正如您所看到的,UITextField 以及整个 Collection View (看不到)随着键盘一起向上推。通常键盘会被覆盖,对 View 没有影响。

之前

Before

之后

After

更新提供代码。我用来实现的方法不涉及 Storyboard。

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow()
window?.makeKeyAndVisible()

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let rvc = ViewController(collectionViewLayout: layout)
window?.rootViewController = rvc

return true
}

// .... other boilerplate code provided by Apple when you make a new App
}

ViewController.swift

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UITextFieldDelegate {
let homeCellId = "homeCellId"
let worldCellId = "worldCellId"

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
collectionView?.register(WorldCell.self, forCellWithReuseIdentifier: worldCellId)
collectionView?.isPagingEnabled = true
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(indexPath.item)
let identifier: String
if indexPath.item == 0 {
identifier = homeCellId
} else if indexPath.item == 1 {
identifier = worldCellId
}
else {
identifier = homeCellId
}

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
cell.backgroundColor = UIColor.blue
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}

MyCell.swift

class MyCell: UICollectionViewCell {

override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func setupView() {

}


}

HomeCell.swift

class HomeCell: MyCell {
let weightField: UITextField = UITextField(frame: .zero)
override func setupView() {
super.setupView()
print("HomeCell")
weightField.backgroundColor = UIColor.white
weightField.translatesAutoresizingMaskIntoConstraints = false
weightField.keyboardType = UIKeyboardType.default
self.addSubview(weightField)

weightField.topAnchor.constraint(equalTo: self.topAnchor, constant: 200).isActive = true
weightField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 50).isActive = true
}


}

WorldCell.swift

//与 HomeCell 相同

class WorldCell: MyCell {

override func setupView() {
super.setupView()
print("worldcell")
}


}

最佳答案

好的,我已经找到了解决这个问题的方法。我在 stackoverflow 上关于类似事件的其他几个线程中遇到了这一点,在一种情况下,答案没有投票,并且对该答案留下的评论说它不起作用..

尽管如此,我仍然不清楚为什么其他实现会导致 Collection View 向上移动。尽管窗口、 Root View Controller 及其 subview 以及键盘之间存在一些相关性。我不知道为什么会发生这种情况。

现在开始编写代码并修复..

上述问题中的方法与此处的主要区别在于 Collection View 的初始化方式。我只会发布我更改的内容,因为其余部分都是一样的。

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let rvc = ViewController()

window? = UIWindow()
window?.makeKeyAndVisible()
window?.rootViewController = rvc
return true
}
}

这里的显着区别是 Root View Controller 不再使用 Collection View Controller 进行初始化。我们使用标准 View Controller 。

ViewController.swift

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

lazy var collView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.dataSource = self
view.delegate = self
return view
}()

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = UIColor.darkGray
collView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
collView.register(HomeCell.self, forCellWithReuseIdentifier: "homeCellId")
self.view.addSubview(collView)
collView.backgroundColor = UIColor.blue
collView.translatesAutoresizingMaskIntoConstraints = false
collView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
collView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
collView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
collView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
collView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1)
// Do any additional setup after loading the view, typically from a nib.
}
}

我们使用 Collection View 作为 subview 初始化 View Controller ,并将我们通常对单元格应用的相同代码

关于ios - UICollectionView - 当键盘出现时,整个 Collection View 随键盘移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51972406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com