更改键盘通知上的视图布局(Swift、iOS)

Posted

技术标签:

【中文标题】更改键盘通知上的视图布局(Swift、iOS)【英文标题】:Change View Layout on Keyboard Notifications (Swift, iOS) 【发布时间】:2016-03-23 17:36:25 【问题描述】:

我正在尝试使用 Swift 为 ios 应用创建登录屏幕。

Here 是我希望它在没有隐藏键盘的情况下的样子。一旦用户点击文本字段,我想更改为看起来像this,并显示键盘。如您所见,它更紧凑

我尝试过自己做,但使用我在网上找到的示例似乎无法弄清楚,因为我在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)

【问题讨论】:

您的部署目标是什么? iOS 9 还是更早的版本? 【参考方案1】:

不幸的是,我无法让它与AutoLayoutConstraints 一起工作,所以我禁用了它并自己完成了所有数学运算。

这是一个小sn-p:

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)

【讨论】:

嗨@aashah7,您不需要添加或删除约束,您可以使用视图之间的乘数(UIImage、UILabel、UITextfield 和 UIButton)来实现所需的输出购买。并在键盘打开时更改滚动视图的高度约束。确保图像视图的约束连接到滚动视图的顶部,并且忘记密码连接到滚动视图的底部

以上是关于更改键盘通知上的视图布局(Swift、iOS)的主要内容,如果未能解决你的问题,请参考以下文章

无法移动键盘视图iOS9 swift

iOS 8 自定义键盘自动布局键

ios 怎样获取在tableview上的键盘高度

键盘显示时更改自动布局常量

在 iOS 中编辑键盘布局

如何更改iOS自定义键盘的高度?