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)
}
Was ist Ihr Implementierungsziel? iOS 9 oder etwas früher? –
Es ist iOS 9, Rob. – aashah7