【发布时间】:2017-08-31 00:57:45
【问题描述】:
Stack Overflow Solution。当我使用
super. viewWillAppear(true)时,我的视图在键盘上看起来很完美,但这里自动滚动会产生编辑UITextField的问题。
在我的UITableView 中,我使用两个cell 一个用于详细信息,另一个用于页脚按钮Sign UP
在键盘前查看图像
在键盘后查看图像
此处为页脚视图代码
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.navigationController?.isNavigationBarHidden = false
getTimeZone()
spinnerInitialization()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "Home_Logo2")
imageView.image = image
navTitle.titleView = UIImageView(image: image)
hideKeyboardWhenTappedAround()
}
可以看到注册按钮的页脚代码
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerCell = tableView.dequeueReusableCell(withIdentifier: "footer") as! CreateAccountTableViewCell
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! CreateAccountTableViewCell
footerCell.signupButtonClick = {
if cell.cmpName.text!.isEmpty || cell.email.text!.isEmpty || cell.firstName.text!.isEmpty || cell.lastName.text!.isEmpty || cell.password.text!.isEmpty || cell.cnfirmPassword.text!.isEmpty {
let dialog = UIAlertController(title: "All Field Required", message: "Please check, All field are required", preferredStyle: UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(ACTION) in
return
}
dialog.addAction(okAction);
DispatchQueue.main.async(execute: {
UIApplication.shared.keyWindow?.rootViewController?.present(dialog, animated: true, completion: nil)
})
}
else {
if cell.password.text! != cell.cnfirmPassword.text! {
let dialog = UIAlertController(title: "Confirm Password does not matched.", message: "", preferredStyle: UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(ACTION) in
return
}
dialog.addAction(okAction);
DispatchQueue.main.async(execute: {
UIApplication.shared.keyWindow?.rootViewController?.present(dialog, animated: true, completion: nil)
})
}
let postString = "cmpname=\(cell.cmpName.text!)&email=\(cell.email.text!)&firstName=\(cell.firstName.text!)&lastName=\(cell.lastName.text!)&password=\(cell.password.text!)&cnfirmPassword=\(cell.cnfirmPassword.text!)&token=\("afdg2015")&signUpFrom=\("IOS")&timezone=\(cell.timezonePickerView.selectedRow(inComponent: 0))¤tPlanId=\(4)"
self.registerMe(postString: postString, password: cell.password.text! )
}
}
return footerCell
}
详细视图的代码,您可以在其中看到编辑文本
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailsCell", for: indexPath) as! CreateAccountTableViewCell
DispatchQueue.main.async(execute: {
cell.timezonePickerView.reloadAllComponents()
cell.timezonePickerView.selectRow(self.idSelect, inComponent: 0, animated: false)
cell.cmpName.underlined()
cell.email.underlined()
cell.firstName.underlined()
cell.lastName.underlined()
cell.password.underlined()
cell.cnfirmPassword.underlined()
cell.cmpName.delegate = self
cell.email.delegate = self
cell.firstName.delegate = self
cell.lastName.delegate = self
cell.password.delegate = self
cell.cnfirmPassword.delegate = self
})
cell.selectionStyle = .none
return cell
}
在这里,当我单击UITextField 时,视图会在任意随机位置自动滚动。我希望滚动应该在用户滚动时起作用,否则滚动应该停止。
我怎样才能完成这项任务...
【问题讨论】:
-
为了正确起见,当键盘出现(即,texfield 已被点击)时,您是否希望禁用滚动?
-
您是否尝试过将表格嵌入到
UIViewController的视图中,而不是使用UITableViewController?我相信在这种情况下表格视图不再自动滚动。 -
@AhmadF 是的,你是对的
I want auto scrolling off when tapped on textfield -
@DonMag 是的,我尝试了两个控制器。但我被困在同一件事上。
-
@DonMag 是一样的,即使使用包含 UITableView 的 UIViewController 也会滚动。
标签: ios swift xcode uitableview uitextfield