这不是我想要的效果。仅当将收集视图设置为水平流布局时,才会发生这种情况。我也看到过其他一些关于同一问题的文章,但提供的答案均无效。有没有人找到解决方案?
我提供了两个屏幕快照,分别显示了在触发键盘之前和之后的UITextField。如您所见,UITextField连同整个集合视图(无法看到)一起随键盘向上推。通常,键盘是覆盖的,对视图没有影响。
之前
之后
更新 提供的代码。我用来实现的方法不涉及情节提要。
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")
}
}
答案 0 :(得分:1)
好的,所以我找到了解决此问题的方法。我在关于类似事件的stackoverflow的其他几个线程中遇到了这个问题,在一个案例中,Answer没有投票归因于此,而对答案的评论则说它不起作用。
尽管如此,我仍然不清楚为什么其他实现会导致集合视图上移。尽管窗口,根视图控制器及其子视图与键盘之间存在一些关联。我不知道为什么会这样。
现在继续代码并修复。.
上面问题中的方法与此处的方法之间的主要区别是收集视图的初始化方式。我只会发布更改过的内容,因为其余的都一样。
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
}
}
此处的显着区别是根视图控制器不再使用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.
}
}
我们将collectionview作为子视图来初始化View Controller,并将通常与单元格相同的代码应用于