2016-03-23 2 views
0

Ich versuche, mit Swift einen Anmeldebildschirm für eine iOS-App zu erstellen.Ansichtslayout auf Tastaturbenachrichtigungen ändern (Swift, iOS)

Here ist, was ich will, damit es aussieht, ohne die Tastatur zu verstecken. Sobald der Benutzer auf ein Textfeld tippt, möchte ich mit der angezeigten Tastatur wie folgt aussehen: this. Wie Sie sehen können, ist es kompaktes

Ich habe versucht, es selbst zu tun, aber kann nicht scheinen, um es herauszufinden, die anhand von Beispielen die ich online gefunden habe, da ich AutoLayoutConstraints in InterfaceBuilder verwenden. Alles ist in eine UIScrollView eingebettet.

Meine Frage ist, mache ich das richtig? Wie in, alle zuvor in InterfaceBuilder gesetzten Einschränkungen entfernen und sie dann programmgesteuert wiederholen, damit es so aussieht, wie ich es möchte.

Gibt es einen besseren Weg, dies zu tun? Das scheint langweilig. Ich bin neu in Swift/iOS-Programmierung, also entschuldige mich, wenn das eine offensichtliche Frage ist. Hier

ist das, was ich getan habe, so weit:


@IBOutlet weak var scrollView: UIScrollView! 
@IBOutlet weak var logoImage: UIImageView! 
@IBOutlet weak var logoText: UILabel! 
@IBOutlet weak var usernameTextField: UITextField! 
@IBOutlet weak var passwordTextField: UITextField! 
@IBOutlet weak var loginButton: UIButton! 
@IBOutlet weak var forgotPasswordButton: UIButton! 
@IBOutlet weak var createAccountButton: UIButton! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    registerForKeyboardNotifications() 
} 

func registerForKeyboardNotifications() { 
     let notificationCenter = NSNotificationCenter.defaultCenter() 
     notificationCenter.addObserver(self, selector: "keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil) 
     notificationCenter.addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil) 
} 

func keyboardWillBeShown(sender: NSNotification) { 
    let userInfo = sender.userInfo! 
    let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height 

    let screenSize: CGRect = UIScreen.mainScreen().bounds   // get screen size 
    let screenWidth = screenSize.width        // ... screen width 
    let screenHeight = screenSize.height       // ... screen height 
    let heightToFit = screenHeight - keyboardHeight     // ... height of screen with keyboard 

    self.view.translatesAutoresizingMaskIntoConstraints = false 

    self.view.removeConstraints(self.view.constraints) 

    self.view.addConstraint(
    NSLayoutConstraint(item: self.scrollView, 
        attribute: .Bottom, 
        relatedBy: .Equal, 
        toItem: self.view, 
        attribute: .Bottom, 
       multiplier: 1.0, 
        constant: -keyboardHeight)) 

    self.view.addConstraint(
    NSLayoutConstraint(item: self.scrollView, 
        attribute: .Top, 
        relatedBy: .Equal, 
        toItem: self.view, 
        attribute: .Top, 
       multiplier: 1.0, 
        constant: 0.0)) 


    self.view.addConstraint(
    NSLayoutConstraint(item: self.scrollView, 
        attribute: .Trailing, 
        relatedBy: .Equal, 
        toItem: self.view, 
        attribute: .Trailing, 
       multiplier: 1.0, 
        constant: 0.0)) 

    self.view.addConstraint(
    NSLayoutConstraint(item: self.scrollView, 
        attribute: .Leading, 
        relatedBy: .Equal, 
        toItem: self.view, 
        attribute: .Leading, 
       multiplier: 1.0, 
        constant: 0.0)) 

    self.view.addConstraint(
    NSLayoutConstraint(item: self.scrollView, 
        attribute: .Height, 
        relatedBy: .Equal, 
        toItem: nil, 
        attribute: .NotAnAttribute, 
       multiplier: 1.0, 
        constant: heightToFit)) 

    self.view.addConstraint(
    NSLayoutConstraint(item: self.view, 
        attribute: .Width, 
        relatedBy: .Equal, 
        toItem: nil, 
        attribute: .NotAnAttribute, 
       multiplier: 1.0, 
        constant: screenWidth)) 


    self.scrollView.setContentOffset(CGPointMake(0, self.usernameTextField.frame.origin.y - 50), animated: true) 
} 

func keyboardWillBeHidden(sender: NSNotification) { 
     let screenSize: CGRect = UIScreen.mainScreen().bounds   // get screen size 
     let screenWidth = screenSize.width        // ... screen width 
     let screenHeight = screenSize.height       // ... screen height 

     self.view.translatesAutoresizingMaskIntoConstraints = true 

     self.view.removeConstraints(self.view.constraints) 

     self.view.addConstraint(
      NSLayoutConstraint(item: self.scrollView, 
          attribute: .Bottom, 
          relatedBy: .Equal, 
          toItem: self.view, 
          attribute: .Bottom, 
         multiplier: 1.0, 
          constant: screenHeight)) 

     self.view.addConstraint(
      NSLayoutConstraint(item: self.scrollView, 
          attribute: .Top, 
          relatedBy: .Equal, 
          toItem: self.view, 
          attribute: .Top, 
         multiplier: 1.0, 
          constant: 0.0)) 

     self.view.addConstraint(
      NSLayoutConstraint(item: self.scrollView, 
          attribute: .Trailing, 
          relatedBy: .Equal, 
          toItem: self.view, 
          attribute: .Trailing, 
         multiplier: 1.0, 
          constant: 0.0)) 

     self.view.addConstraint(
      NSLayoutConstraint(item: self.scrollView, 
          attribute: .Leading, 
          relatedBy: .Equal, 
          toItem: self.view, 
          attribute: .Leading, 
         multiplier: 1.0, 
          constant: 0.0)) 

     self.view.addConstraint(
      NSLayoutConstraint(item: self.scrollView, 
          attribute: .Height, 
          relatedBy: .Equal, 
          toItem: nil, 
          attribute: .NotAnAttribute, 
         multiplier: 1.0, 
          constant: screenHeight)) 

     self.view.addConstraint(
      NSLayoutConstraint(item: self.view, 
          attribute: .Width, 
          relatedBy: .Equal, 
          toItem: nil, 
          attribute: .NotAnAttribute, 
         multiplier: 1.0, 
          constant: screenWidth)) 


    self.scrollView.setContentOffset(CGPointMake(0, -self.usernameTextField.frame.origin.y + 50), animated: true) 
} 
+0

Was ist Ihr Implementierungsziel? iOS 9 oder etwas früher? –

+0

Es ist iOS 9, Rob. – aashah7

Antwort

0

Leider konnte ich es nicht mit der AutoLayoutConstraints arbeiten, also habe ich es deaktiviert und habe die ganze Mathematik selbst gemacht.

Hier ist ein kleiner Ausschnitt:

let screenSize: CGRect = UIScreen.mainScreen().bounds   // get screen size 
let screenWidth = screenSize.width        // ... screen width 
let screenHeight = screenSize.height       // ... screen height 
let padding: CGFloat = 30          // space on left and right 
let adjustedViewWidth = screenWidth - 2*padding 

self.scrollView.frame = CGRectMake(0, 0, screenWidth, screenHeight) 

self.logoImage.frame = CGRectMake(screenWidth/2 - self.logoImage.frame.width/2, self.logoImage.frame.origin.y, self.logoImage.frame.width, self.logoImage.frame.height) 
self.logoText.frame = CGRectMake(screenWidth/2 - self.logoText.frame.width/2, self.logoText.frame.origin.y, self.logoText.frame.width, self.logoText.frame.height) 
self.usernameTextField.frame = CGRectMake(screenWidth/2 - adjustedViewWidth/2, self.usernameTextField.frame.origin.y, adjustedViewWidth, self.usernameTextField.frame.height) 
self.passwordTextField.frame = CGRectMake(screenWidth/2 - adjustedViewWidth/2, self.passwordTextField.frame.origin.y, adjustedViewWidth, self.passwordTextField.frame.height) 
self.loginButton.frame = CGRectMake(screenWidth/2 - adjustedViewWidth/2, self.loginButton.frame.origin.y, adjustedViewWidth, self.loginButton.frame.height) 
+0

Hi @ aashah7, Sie müssen keine Einschränkungen hinzufügen oder entfernen, Sie können den gewünschten Out-Put-Kauf mit dem Multiplikator zwischen den Ansichten erreichen (UIImage, UILabel, UITextfield & UIButton). und Ändern der Höhenbeschränkung der Bildlaufansicht, wenn die Tastatur hochgefahren ist. Stellen Sie sicher, dass die Einschränkung der Bildansicht mit dem oberen Rand der Bildlaufansicht verbunden ist und dass Kennwort vergessen mit dem unteren Rand der Bildlaufansicht verbunden ist –