4

Stack Overflow Solution. When i use 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

BEFORE KeyBoard

View Image After KeyBoard

After 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...

Community
  • 1
  • 1
Pushpendra
  • 1,492
  • 1
  • 17
  • 33
  • To get it right, is what are you looking for disabling scrolling when the keyboard appears (i.e, texfield has been tapped)? – Ahmad F Apr 05 '17 at 12:13
  • Have you tried embedding the table in the view of a `UIViewController` instead of using a `UITableViewController`? I *believe* the table view no longer auto-scrolls in that case. – DonMag Apr 05 '17 at 12:14
  • @AhmadF yes you are right `I want auto scrolling off when tapped on textfield` – Pushpendra Apr 05 '17 at 12:16
  • @DonMag Yes i try both Controller. But i am stuck on the same thing. – Pushpendra Apr 05 '17 at 12:16
  • @DonMag it is the same, it will scroll even when using UIViewController containing a UITableView. – Ahmad F Apr 05 '17 at 12:20
  • but after the keyboard appeared, scrolling should be enabled, right? also, why you want to disable such a cool feature? it is a good practice to let the table view to scroll to let the user the selected text field :) – Ahmad F Apr 05 '17 at 12:23
  • @AhmadF i think you are not getting my point. Each an every thing is working great till i am not click any `textField` as i click on any text field then their is autoscrolling creating problem. let suppose if i click on company name then company name get `hide` behind the navigator bar. – Pushpendra Apr 05 '17 at 12:27
  • @APKAPPS - I just did a quick test... `UITableView` embedded in the view of a standard `UIViewController` ... the table does **NOT** auto-scroll when the keyboard appears. If I start editing a cell at the bottom, it is covered by the keyboard. – DonMag Apr 05 '17 at 12:30
  • @DonMag yes the same thing in that i can do by removing `super.viewWillAppear(true)` from `func viewWillAppear` but i also want footer on the top of the Keyboard. – Pushpendra Apr 05 '17 at 12:33
  • @APKAPPS - ok, I think you need to explain better what you want, and what is not working... Your two images don't seem to match what you are asking... Do you simply want the green "Sign Up" button to sit at the bottom of the view, and move up with the keyboard? – DonMag Apr 05 '17 at 12:36
  • @DonMag When i click on the `TextField` then auto scrolling of `UITableView` is creating problem in view looks. Means When i click on `Company Name` then view auto goes up and field get hide behind top view where back icon shows. If i click on `Confirm Password` then view goes behind key board automatically . – Pushpendra Apr 05 '17 at 12:42
  • @APKAPPS - that's what I *thought* you were saying... that **Auto-Scrolling** of your `UITableView` was causing problems. My response was that if you do **NOT** use a `UITableViewController` then there is **NO** Auto-Scrolling. Is that not what you are asking? – DonMag Apr 05 '17 at 12:50
  • @DonMag But how i can bring Sign-UP Button on the top of Keyboard. I want my butting as shown in second image. And rest view would be same as it is now with scrolling – Pushpendra Apr 05 '17 at 13:05

1 Answers1

0
  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.

DonMag
  • 69,424
  • 5
  • 50
  • 86