【澳门葡京备用网址】至于键盘遮住输入框的化解办法,开拓之路

      在求学开辟登入页的时候,蒙受的标题分享如下:

      在读书开拓登入页的时候,遭受的标题分享如下:

        做app开荒,平时会蒙受输入框被键盘遮住的事态,譬如注册分界面有诸多新闻须要输入,点击靠荧屏下方的输入框就会超出输入框被键盘遮住的意况。查阅网络的累累质地,有的是仅依据UITextField的八个代理方法加上写死键盘的可观来消除的,还有的是依赖四个第一方库IQKeyboardManager,依赖第二方库尽管节省了许多开支时间,然而如若你的类型仅仅只是注册分界面一个分界面用到,导入库反而未有自个儿几句代码写的便利。上面笔者把温馨的化解办法介绍一下,供我们参考。

appdelegate.swift

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

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

(注释:下边代码应用斯维夫特语言,OC语言同理)

增添属性

  @IBOutlet weak var bottomConstraint: NSLayoutConstraint【澳门葡京备用网址】至于键盘遮住输入框的化解办法,开拓之路。!  
 //注意那里要在storyboard对最尾巴部分的控件设置约束,然后连线到.swift文件实行绑定

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

粗粗思路:使用键盘通告UIKeyboard威尔Show和UIKeyboard威尔Hide,合作UITextFieldDelegate代理方法func
textFieldShouldBegin艾德iting(_ text菲尔德: UITextField) ->
Bool化解。完成如下图所示的分界面:

var allowRotation = false

  override func viewDidLoad() {

  override func viewDidLoad() {

澳门葡京备用网址 1

增添办法

        super.viewDidLoad()

        super.viewDidLoad()

邮箱注册

func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) ->
UIInterfaceOrientationMask {

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

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

只顾:假诺是小显示器手提式有线话机的话,按上边五个输入框的时候,键盘就会遮住输入框

if allowRotation {

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

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

要专注多个难点:1、不是负有系统键盘中度都以如出一辙的,须要去动态获取 

return .all  // 帮衬活动横竖屏, 
假如急需强制横竖屏就设置landscapeLeft/landscapeRight

    } 

    } 

二、键盘文告和代理方法textFieldShouldBegin艾德iting的调用是代理方法先调用,键盘通告后调用

}else {

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

    点击空白处撤废弹出的键盘

杀鸡取卵方案:

return .portrait

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

@IBOutlet weak var scrollView: UIScrollView!

}

    键盘改动事件,幸免键盘遮住输入框

    键盘改动事件,幸免键盘遮住输入框

private var textFieldTmp:UITextField?

}

    // 键盘改变
    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, width: 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)
        }
    }
    // 键盘改变
    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, width: 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)
        }
    }

override func viewDidLoad() {

假设不行调节器须求援助横竖屏 。 

    后续显明还会蒙受大多主题素材,笔者会尽量把本人蒙受的标题以及消除办法记录下来供后来者学习。

    后续确定还会碰着不少主题材料,笔者会尽量把自身遇见的题材以及消除办法记录下来供后来者学习。

        super.viewDidLoad()

let appD = UIApplication.shared.delegate as? AppDelegate

    顺便嘲谑一下,从安卓转过来,发掘诸多标题交给的消除方案都是Object-C ,而且 斯威夫特版本更迭,变化非常大,网络海人民广播广播台湾大学的缓和方案通常索要和谐微小调度才具健康使用。给本身这么的新手带来大多难为。

    顺便调侃一下,从安卓转过来,发掘很多主题材料提交的消除方案都以Object-C ,而且 Swift版本更迭,变化非常大,网络海人民广播电视台湾大学的缓和方案平时供给本身微薄调解才干健康使用。给本身这么的新手带来繁多劳苦。

//一定要写上代理,不然上边包车型大巴textFieldDelegate方法不会进行

appD.allowRotation = true

    注:开辟条件是Xcode 八.一   测试平台是 iOS 拾.0 

    注:开垦条件是Xcode 8.1   测试平台是 iOS 10.0 

        tfEmail.delegate = self

监听横竖屏事件

        tfAuthCode.delegate = self

override func viewDidLoad() {

        tfPWD.delegate = self

super.viewDidLoad()

        tfConfirmPWD.delegate = self

NotificationCenter.default.addObserver(self, selector:
#selector(orinationNotification), name: .UIDeviceOrientationDidChange,
object: nil)

        tfRealName.delegate = self

}

        NotificationCenter.default.addObserver(self, selector:
#selector(keyboardWillAppear(_澳门葡京备用网址,:)), name:
NSNotification.Name.UIKeyboardWillShow, object: nil)

@objc fileprivate func orinationNotification() {

        NotificationCenter.default.addObserver(self, selector:
#selector(keyboardWillDisapear(_:)), name:
NSNotification.Name.UIKeyboardWillHide, object: nil)

let orient = UIDevice.current.orientation

}

switch orient {

func keyboardWillAppear(_ notification:Notification){//键盘出现

case .landscapeLeft, .landscapeRight:

        let keyboardInfo =
notification.userInfo![UIKeyboardFrameBeginUserInfoKey]

view.backgroundColor = UIColor.red

        let keyboardHeight = (keyboardInfo! as
AnyObject).cgRectValue.size.height//键盘中度

self.navigationController?.navigationBar.isHidden = true

       let rect = textFieldTmp?.convert((textFieldTmp?.frame)!, to:
self.scrollView)//那里要看上边的那个textFiled是或不是嵌套在别的view上,不然获取的rect是不标准的

default:

        if kScreenHeight – kNavigationAndStatusBarHeight –
((rect?.origin.y)! + 44) < keyboardHeight {

view.backgroundColor = UIColor.green

            let yOffset = keyboardHeight – (kScreenHeight –
kNavigationAndStatusBarHeight – ((rect?.origin.y)! + 44))

self.navigationController?.navigationBar.isHidden = false

            self.scrollView.setContentOffset(CGPoint(x: 0, y: yOffset),
animated: true)

}

        }

}

    }

    func keyboardWillDisapear(_ notification:Notification) {//键盘消失

        self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated:
true)

    }

///mark – UITextFieldDelegate

    func textFieldShouldBeginEditing(_ textField: UITextField) ->
Bool {

        textFieldTmp = textField

        return true

    }

盼望对大家全部支持

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website