我正试图使用Swift为iOS应用程序创建一个登录屏幕。
这是我想要的没有隐藏键盘的样子。一旦用户点击一个文本字段,我想把它改成这样,并显示键盘。正如你所看到的,它是更紧凑的
我试着自己做,但似乎无法用我在网上找到的例子来解决,因为我在InterfaceBuilder
中使用AutoLayoutConstraints
。所有内容都嵌入到UIScrollView
中。
我的问题是,我这样做正确吗?与中一样,删除之前在InterfaceBuilder
中设置的所有约束,然后以编程方式重新进行它们,使其看起来像我想要的那样。
有更好的方法吗?这似乎很乏味。我是Swift/iOS编程的新手,所以如果这是一个明显的问题,我很抱歉。
以下是我迄今为止所做的:
@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)
}
不幸的是,我无法让它与AutoLayoutConstraints
一起工作,所以我禁用了它,并自己做了所有的数学运算。
这里有一个小片段:
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)