简体   繁体   中英

how to stop scrolling in uitableview while editing uitextfield

Stack Overflow Solution . When i use super. viewWillAppear(true) super. viewWillAppear(true) then my view looks perfect with keyboard but here auto scrolling create problem with editing of UITextField .

In my UITableView i am using two cell one for details and another for footer button Sign UP

View Image Before KeyBoard

在KeyBoard之前

View Image After KeyBoard

KeyBoard之后

Here Code for Footer View

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()
}

Code For Footer where you can see the Sign Up Button

    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))&currentPlanId=\(4)"
            self.registerMe(postString: postString, password: cell.password.text! )
        }
    }
    return footerCell
}

Code For Detail View Where You can see Edit Text

 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
}

Here when i click on UITextField the view automatic scroll at any random position. I want Scrolling should be work when user do scroll other wise scrolling should be stop.

How i can achieve that task...

  1. Use a regular UIViewController instead of a UITableViewController
  2. Add your UITableView as a subview of the main View.
  3. Add your SIGN UP button as another subview of the main View, constrained to the bottom of the main View.
  4. Add handling for keyboard WillShow / DidShow / WillHide / DidHide notifications as necessary. When the keyboard is sliding in or out of view, animate the change in position of your sign up button view at the same time.

This will disable the auto-scrolling of the table view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM