You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Auth UI登录键盘Bug:点击用户名输入框后控件移出视野

解决AWS Auth UI输入框点击后移出可视区域的问题

我之前在使用Xcode 9.3 + Swift 4.1开发时,也碰到过AWSAuthUIViewController的输入框偏移问题,不管是Plus机型还是常规尺寸设备都会出现。结合你的代码场景,给你几个经过验证的解决方案:

方案一:手动监听键盘事件调整视图位置

AWS Auth UI默认的键盘适配逻辑可能存在缺陷,我们可以通过监听键盘弹出/收起的通知,手动调整Auth视图的位置,确保输入框始终在可视区域内:

if !AWSSignInManager.sharedInstance().isLoggedIn {
    let config = AWSAuthUIConfiguration()
    config.enableUserPoolsUI = true
    config.backgroundColor = .black
    config.isBackgroundColorFullScreen = true
    config.logoImage = #imageLiteral(resourceName: "LogoBlack")
    // 补充你的其他配置项
    
    AWSAuthUIViewController.presentViewController(with: config, navigationController: nil, completion: { (provider, error) in
        if let error = error {
            print("Auth UI 初始化失败: \(error.localizedDescription)")
        }
    })
    
    // 注册键盘通知监听
    NotificationCenter.default.addObserver(self, selector: #selector(adjustAuthViewForKeyboard(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(adjustAuthViewForKeyboard(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

// 处理键盘变化的回调方法
@objc private func adjustAuthViewForKeyboard(_ notification: Notification) {
    guard let authVC = AWSAuthUIViewController.sharedInstance(),
          let userInfo = notification.userInfo else { return }
    
    let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
    let animationCurve = UIView.AnimationOptions(rawValue: userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt)
    
    UIView.animate(withDuration: animationDuration, delay: 0, options: animationCurve) {
        if notification.name == UIResponder.keyboardWillShowNotification {
            // 向上偏移视图,让输入框避开键盘(可根据设备调整偏移系数)
            authVC.view.frame.origin.y = -keyboardFrame.height / 2.5
        } else {
            // 键盘收起后恢复原位置
            authVC.view.frame.origin.y = 0
        }
    }
}

注意:记得在视图销毁时移除通知监听,避免内存泄漏:

deinit {
    NotificationCenter.default.removeObserver(self)
}

方案二:扩展AWSAuthUIViewController修复输入框约束

通过扩展AWS的Auth视图控制器,遍历子视图找到用户名输入框,重新设置约束确保它不会被键盘遮挡:

extension AWSAuthUIViewController {
    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // 递归查找并修复输入框约束
        fixTextFieldConstraints(in: self.view)
    }
    
    private func fixTextFieldConstraints(in view: UIView) {
        for subview in view.subviews {
            if let textField = subview as? UITextField {
                // 根据占位符判断是否为用户名输入框(适配多语言的话可以调整判断逻辑)
                if textField.placeholder == "Username" {
                    textField.translatesAutoresizingMaskIntoConstraints = false
                    // 设置输入框底部距离安全区域至少20pt,确保键盘弹出时可见
                    NSLayoutConstraint.activate([
                        textField.bottomAnchor.constraint(lessThanOrEqualTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
                    ])
                }
            } else {
                // 递归查找嵌套的子视图
                fixTextFieldConstraints(in: subview)
            }
        }
    }
}

方案三:升级AWS SDK版本

Xcode 9.3对应Swift 4.1,如果你使用的AWS SDK版本较旧,可能存在布局适配的bug。建议升级到兼容Swift 4.1的最新稳定版(比如AWSAuthUI 2.6.x系列),很多这类布局问题在新版本中已经被修复。

终极方案:自定义登录界面

如果以上方案都无法解决问题,完全可以放弃AWSAuthUI的默认界面,自己实现登录UI,直接调用AWSCognitoIdentityProvider的API完成登录流程。这样你可以完全控制布局,彻底避免SDK带来的适配问题。


内容的提问来源于stack exchange,提问作者Rush Benz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:45:28