使用 Swift 通过键盘移动视图

2024-01-21

我有一个应用程序,在视图的下半部分有一个文本字段。 这意味着当我在文本字段中输入内容时,键盘会覆盖文本字段。

我如何在打字时向上移动视图,以便可以看到我正在输入的内容,然后在键盘消失时将其移回到原来的位置?

我到处都看过,但所有解决方案似乎都在 Obj-C 中,我还不能完全转换。

任何帮助将不胜感激。


这是一种解决方案,无需处理从一个文本字段到另一个文本字段的切换:

override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)            
    }   

func keyboardWillShow(notification: NSNotification) {            
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        self.view.frame.origin.y -= keyboardSize.height
    }            
}

func keyboardWillHide(notification: NSNotification) {
    self.view.frame.origin.y = 0
}

要解决这个问题,替换两个函数keyboardWillShow/Hide用这些:

func keyboardWillShow(notification: NSNotification) {        
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        if view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }        
}

func keyboardWillHide(notification: NSNotification) {
    if view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}

斯威夫特 3.0:

override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    
}

@objc func keyboardWillShow(notification: NSNotification) {        
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }        
}

@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}
    

斯威夫特 4.0:

override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    
}

@objc func keyboardWillShow(notification: NSNotification) {        
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }        
}

@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}

斯威夫特 4.2:

override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Swift 通过键盘移动视图 的相关文章

  • 修改 SCNParticleEventBlock 中的 SCNParticleSystem 颜色不起作用

    鉴于提供的示例代码handle forProperties handler https developer apple com documentation scenekit scnparticlesystem 1523251 handle
  • iOS 9.3 中的 KVO 被破坏

    这可能是 iOS 9 3 发行版 中的一个可怕的错误 添加单个观察者时 NSUserDefaults standardUserDefaults 我注意到响应方法 observeValueForKeyPath ofObject change
  • iOS 和 Firebase 自动续订订阅

    我的问题 我很难找到一种使用 Firebase 在 iOS 中安全管理自动续订订阅的方法 购买流程 User1 purchases a subscription 使用订阅标识符更新 Firebase 上 User1 的帐户 用于解锁内容 存
  • 如何在气隙 Mac 上安装新的 Apple 全球开发者关系中级证书?

    您可能知道也可能不知道 现在使用新的中间证书生成新的签名证书 你可以在这里读到它 https developer apple com support wwdr intermediate certificate https developer
  • UICollectionViewCell 拖动预览的自定义视图

    我正在尝试实现一项功能 用户可以将一个集合视图单元格拖放到另一个集合视图单元格上 但是 我想完全更改运动中物体的预览 以匹配我的应用程序的视觉隐喻 该项目没有移动 该项目包含的东西正在移动 例如 假设我的collectionview单元格显
  • iOS9 Sprite 套件问题

    一切都很顺利 直到我升级到 xCode 7 和 iOS 9 我当前的项目是一个 2D 平台游戏 自从升级以来 我就陷入了我们许多人似乎都面临的精灵套件错误 错误的困扰 我的问题是 每次游戏在模拟器或设备上运行时 所有精灵的 zPositio
  • 如何在javascript中计算日出和日落?

    我正在使用appcelerator titan开发一个IOS应用程序 我想让我的应用程序在日出和日落时向用户发送本地通知 解决这个问题的一个好工具是使用 YQL 的雅虎天气 但是 雅虎天气仅供非商业用途 我正在尝试找到一个javascrip
  • 核心数据executeFetchRequest消耗大量内存

    我正在核心数据数据库中插入 cca 100 000 条记录 数据库包含 3 个实体 球员 俱乐部 球员俱乐部 实体之间存在关系 玩家 gt 玩家俱乐部俱乐部 在 PlayerClub 中插入时 我注意到插入大约 50 000 条记录后会消耗
  • 如何使用 SwiftUI 使按钮可拖动/可移动?

    我正在尝试使用 SwiftUI 制作一个可移动的按钮 从看起来这应该可行 我尝试将带有文本的按钮放入另一个 ZStack 中 有一秒钟它可以工作 但一旦我释放按钮 拖动就会停止 我无法再拖动 我注意到尽管按钮已经移动 但水龙头仍然位于中心
  • 如何将 RGB 值转换为十六进制字符串 iOS swift

    我想将 RGB 值转换为十六进制字符串 我将十六进制转换为 RGB 如下所示 但反之亦然 func hexStringToRGB hexString String gt red CGFloat green CGFloat blue CGFl
  • 我的 iPhone 6 获取 iPhone 5 媒体查询

    我不明白这里发生了什么事 我在 CSS 媒体查询中专门针对 iphone 5 media only screen and min device width 320px and max device width 568px some div
  • 在 XCode 中本地化 HTML 文件

    我有一个本地化的 iOS 应用程序 我希望在其中包含一些本地化的 HTML 文件 我不知道该怎么做 目前 我的文件夹结构如下所示 myapp en lrproj Localizable strings fr lrproj Localizab
  • AST 文件格式错误或损坏

    我有一个问题 我不知道为什么会发生这种情况 但很可能是因为我错误地按了 移动到垃圾箱 到某些系统框架 我收到一条错误消息 AST 文件格式错误或损坏 找不到 AST 文件引用的文件 Users username myProject Quar
  • 在 for 循环中为元组赋值

    struct MIDIPacket 中有一个 UInt8 的元组 正常的赋值是这样的 import CoreMIDI let packet MIDIPacket packet data 0 0x02 packet data 1 0x5f 等
  • tableView.cellForRowAtIndexPath(indexPath) 返回 nil

    我有一个循环遍历表视图的验证函数 问题是它在某个时刻返回 nil 单元格 for var section 0 section lt self tableView numberOfSections section for var row 0
  • 如何动态添加XCTestCase

    我正在为一个白标签项目编写 UI 测试 其中每个应用程序都有一组不同的菜单项 测试点击每个菜单项并截取屏幕截图 使用快车道快照 https docs fastlane tools actions snapshot 目前这一切都发生在一个内部
  • iOS 上的推送通知渐进式 Web 应用程序

    我需要开发一个集成了推送通知的渐进式网络应用程序 在网上搜索我发现了关于这个主题的不同意见 如果我理解正确的话 目前我们无法在移动版 safari 中推送通知 但仅限桌面版 这样对吗 你有什么建议来获得相同的结果吗 我不是iOS专家 我想知
  • Objective-C 中 NSURL 为 null 而 NSString 是正确的

    我有一个NSString包含一个 url 以及当我分配时NSURL与NSString NSURL 输出 空 这是因为url中有一些非法字符 导致NSURL不编码就无法读取NSString包含网址 NSString u incomingUrl
  • 出现错误:FT_Open_Face 失败:错误 2

    当我使用时出现以下错误CGContextDrawPDFPage context PDFPage 对于某些文件 有解决办法来解决这个问题吗 FT Open Face failed error 2 错误2看起来像errno2 这是 找不到文件
  • 在 Swift 中将 xib 分配给 UIView

    在 Objective C 中 它可以在 init 方法中完成 id init self NSBundle mainBundle loadNibNamed ViewBtnWishList owner 0 options nil object

随机推荐