zoukankan      html  css  js  c++  java
  • iOS 开发之路(登陆页键盘遮挡输入框问题)一

          在学习开发登陆页的时候,遇到的问题分享如下:

        首先是swift 3.0 中,NotificationCenter 设置 selector 如下:

      @IBOutlet weak var bottomConstraint: NSLayoutConstraint!    //注意这里要在storyboard对最底部的控件设置约束,然后连线到.swift文件进行绑定

      override func viewDidLoad() {

            super.viewDidLoad()

            NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillChange(notification:)),  

                 name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

        } 

        点击空白处取消弹出的键盘

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            self.view.endEditing(true)
        }

        键盘改变事件,防止键盘遮住输入框

        // 键盘改变
        func keyboardWillChange(notification: NSNotification) {
            if let userInfo = notification.userInfo,
                let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
                let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
                let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
                
                let frame = value.cgRectValue
                
                var intersection = frame.intersection(self.view.frame)
                //当键盘消失,让view回归原始位置
                if intersection.height == 0.0 {
                    intersection = CGRect(x: intersection.origin.x, y: intersection.origin.y,  intersection.width, height: 100)
                }
                UIView.animate(withDuration: duration, delay: 0.0,
                                           options: UIViewAnimationOptions(rawValue: curve), animations: {
                                            _ in
                                            //改变下约束
                                            self.bottomConstraint.constant = intersection.height
                                            self.view.layoutIfNeeded()
                }, completion: nil)
            }
        }

        后续肯定还会遇到很多问题,我会尽量把我遇到的问题以及解决办法记录下来供后来者学习。

        顺便吐槽一下,从安卓转过来,发现好多问题给出的解决方案都是 Object-C ,而且 Swift 版本更迭,变化很大,网上很多的解决方案经常需要自己微小调动才能正常使用。给我这样的新手带来好多麻烦。

        注:开发环境是Xcode 8.1   测试平台是 iOS 10.0 

  • 相关阅读:
    H264学习第一篇(编码结构分析)
    mybatis.xml文件中#与$符号的区别以及数学符号的处理
    mybatis批量插入返回主键问题
    mybais整合的框架没有sql-debug输出
    jQuery选择器方式-用的不多的name选择器
    css样式增加&改变颜色
    jstl-vaStatus 属性count与index
    highchart导出功能的介绍更改exporting源码
    bootstrap-validator验证问题总结
    bootstrap-table 加载不了数据问题总结
  • 原文地址:https://www.cnblogs.com/jiajin/p/6020713.html
Copyright © 2011-2022 走看看