2016-06-29 19 views
5

Dopo aver provato la maggior parte delle soluzioni pubblicate qui, sto ancora avendo problemi a spostare il campo di testo per mostrare sopra la tastiera in una scrollview.Swift: ScrollRectToVisible non funziona

Questi sono i link che ho seguito da soluzioni StackOverflow: Link 1 Link 2 Link 3

sto lavorando su uno schermo di iscrizione che ha 1 campo dietro la tastiera quando si presenta.

Ecco il mio codice:

class SignUpViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate, UIPopoverPresentationControllerDelegate { 

@IBOutlet var firstNameTextField: UITextField! 
@IBOutlet var lastNameTextField: UITextField! 
@IBOutlet var phoneNumberTextField: UITextField! 
@IBOutlet var emailTextField: UITextField! 
@IBOutlet var submitButton: UIButton! 
@IBOutlet var professionButton: UIButton! 

var scrollView: UIScrollView? 
var activeTextField:UITextField? = UITextField() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let notificationCenter = NSNotificationCenter.defaultCenter() 

    notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWasShown(_:)), name: UIKeyboardWillShowNotification, object: nil) 
    notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil) 
    scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)) 
    scrollView!.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height) 

    defaultSettings() 
} 

func defaultSettings() { 
    self.firstNameTextField.delegate = self 
    self.lastNameTextField.delegate = self 
    self.emailTextField.delegate = self 
    self.phoneNumberTextField.delegate = self 
} 

func deregisterFromKeyboardNotifications() 
{ 
    //Removing notifies on keyboard appearing 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) 
} 

func keyboardWasShown(notification: NSNotification) 
{ 
    //Need to calculate keyboard exact size due to Apple suggestions 
    //self.scrollView!.scrollEnabled = true 
    var info : NSDictionary = notification.userInfo! 
    var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size 
    var contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0) 

    self.scrollView!.contentInset = contentInsets 
    self.scrollView!.scrollIndicatorInsets = contentInsets 

    var aRect : CGRect = self.view.frame 
    aRect.size.height -= keyboardSize!.height 
    if (!CGRectContainsPoint(aRect, activeTextField!.frame.origin)) 
    { 
//   print(activeTextField?.frame) 
//   var scrollPoint = CGPointMake(0.0, activeTextField!.frame.origin.y - (keyboardSize!.height-15)) 
     self.scrollView!.scrollRectToVisible((activeTextField?.frame)!, animated: true) 
     //self.scrollView?.setContentOffset(scrollPoint, animated: true) 
    } 
} 


func keyboardWillBeHidden(notification: NSNotification) 
{ 
    //Once keyboard disappears, restore original positions 
    //var info : NSDictionary = notification.userInfo! 
    //var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size 
    var contentInsets : UIEdgeInsets = UIEdgeInsetsZero 
    self.scrollView!.contentInset = contentInsets 
    self.scrollView!.scrollIndicatorInsets = contentInsets 
//  self.view.endEditing(true) 
//  self.scrollView!.scrollEnabled = false 

} 

func textFieldDidBeginEditing(textField: UITextField) 
{ 
    activeTextField = textField 
} 

func textFieldDidEndEditing(textField: UITextField) 
{ 
    activeTextField = nil 
} 

Come potete vedere, ho provato scrollRectToVisible con telaio e setContentOffset con Point. Entrambi non hanno funzionato. Mentre il codice seleziona l'emailTextField come campo di testo nascosto.

+1

Usa TPKeyboardAvoiding https://github.com/michaeltyson/TPKeyboardAvoiding. È super facile, non si prenderà cura di tutti. –

+0

@BharatModi, grazie per il tuo commento. Non volevo utilizzare l'API di terze parti, cercando di utilizzare il metodo consigliato da Apple. –

risposta

6

Ero anche alle prese con lo stesso problema che hai fatto, non so se avevi successo e trovato la soluzione, ma alla fine ho usato la funzione setContentOffset invece di scrollRectToVisible e ha funzionato.

Swift esempio 3.x:

if (!aRect.contains(activeTextView!.frame.origin)) { 
       self.scrollView.setContentOffset(CGPoint(x:0, y:self.activeTextView!.frame.origin.y), animated: true) 
} 
Problemi correlati