[英]Change View Layout on Keyboard Notifications (Swift, iOS)
我正在嘗試使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.