在iOS中模拟提醒应用程序的“添加”页脚

无论是通过Interface Builder和/或代码,我都可以使用一些指针来尝试创建类似于iPhone上的Reminders应用程序的“添加”页脚。

更具体地说,我有一个数据列表,并希望始终有一个可编辑的“添加”行,如下所示:

  • 当列表的底部可见时,您会在底部看到“添加”
  • 当不可见时,“添加”行将浮动并在视图底部保持可见/可访问
  • 显示键盘时,“添加”行位于键盘上方

到目前为止,我已经尝试使用表视图控制器并将EditFieldView放在表脚注中。这照顾一个功能。但是我不确定如果不编写代码从那里去哪里。我还是iOS UI的新手,希望避免重新发明已经存在的功能。

布兰登

使视图位于键盘上方但在键盘不可见时仍位于表格底部的示例。

我添加了一个红色的UIView。当您点击它时,键盘将显示出来,并且您可以看到它在键盘上方的位置动画。当您点击单元格本身时,您会看到键盘关闭,并且视图移回到表格的底部。

还有其他方法可以执行此操作,例如使用“键盘附件”视图和底部附带的视图。但是,我不会涉及到这一点。

使用键盘通知的示例:

//
//  ViewController.swift
//  SO
//
//  Created by Brandon T on 2017-02-03.
//  Copyright © 2017 XIO. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var table: UITableView!
    var tableFooter: UIView!
    var bottomConstraint: NSLayoutConstraint!
    var dummyTextField: UITextField?

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //Notifications
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)

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

        //Setup Views
        self.table = UITableView(frame: self.view.frame, style: .grouped)
        self.table.delegate = self
        self.table.dataSource = self


        self.tableFooter = UIView()
        self.tableFooter.backgroundColor = UIColor.red
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(onViewTap(recognizer:)))
        recognizer.numberOfTapsRequired = 1
        recognizer.numberOfTouchesRequired = 1
        self.tableFooter.addGestureRecognizer(recognizer)


        //Constraints
        self.view.addSubview(self.table)
        self.view.addSubview(self.tableFooter)

        self.table.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        self.table.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        self.table.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

        self.tableFooter.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        self.tableFooter.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        self.tableFooter.topAnchor.constraint(equalTo: self.table.bottomAnchor).isActive = true
        self.tableFooter.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
        self.bottomConstraint = self.tableFooter.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        self.bottomConstraint.isActive = true


        self.table.translatesAutoresizingMaskIntoConstraints = false
        self.tableFooter.translatesAutoresizingMaskIntoConstraints = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func keyboardWillShow(notification: Notification) {
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
            let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)

            self.view.layoutIfNeeded()

            UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
                if (self.bottomConstraint.constant <= 0.0) {
                    self.bottomConstraint.constant = -endFrame!.size.height
                }
                self.view.layoutIfNeeded()
            }, completion: { (finished) in

            })
        }
    }

    func keyboardWillHide(notification: Notification) {
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
            let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)

            self.view.layoutIfNeeded()

            UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
                self.bottomConstraint.constant = 0.0
                self.view.layoutIfNeeded()
            }, completion: { (finished) in

            })
        }
    }


    //TableView
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100.0
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.00001
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.00001
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellID")

        if (cell == nil) {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cellID")
            cell!.selectionStyle = .none

            self.dummyTextField = UITextField(frame: CGRect(x: 100, y: 25.0, width: 100, height: 50))
            cell!.contentView.addSubview(self.dummyTextField!)
        }

        cell!.backgroundColor = UIColor.white
        cell!.textLabel?.text = "Test - \(indexPath.row)"



        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.dummyTextField!.resignFirstResponder()
        self.dummyTextField!.endEditing(true)

        self.view.resignFirstResponder()
        self.view.endEditing(true)
    }

    func onViewTap(recognizer: UITapGestureRecognizer) {
        self.dummyTextField?.becomeFirstResponder()
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Mac上的提醒中添加包含特定应用程序的提醒?

来自分类Dev

iOS如何设置应用程序提醒

来自分类Dev

用于模拟提议的示例Java Web应用程序(包括数据库脚本等)

来自分类Dev

片段应用程序,如何添加页脚?

来自分类Dev

类似于提醒应用程序iOS 7中的页面控制

来自分类Dev

如何在 Windows 10 中模拟 iOS 应用程序

来自分类Dev

无法在 iOS 模拟器中安装应用程序

来自分类Dev

如何提醒用户iOS应用程序的新更新

来自分类Dev

vuejs 应用程序中的粘性页脚

来自分类Dev

如何从基于Azure的MVC应用程序中启用提醒?

来自分类Dev

如何模拟iOS应用程序的更新

来自分类Dev

我正在使用4.3版Android中的listview交叉应用程序的listview添加/删除页脚?

来自分类Dev

为整个Android应用程序添加页脚

来自分类Dev

让用户能够在 iOS 中添加应用程序后台功能

来自分类Dev

为什么从iOS模拟器中打开我的iOS应用程序会失败/关闭?

来自分类Dev

即将提交应用程序提醒

来自分类Dev

在Android应用程序内提示或提醒

来自分类Dev

提醒打开应用程序提供反馈

来自分类Dev

如何从 iOS 11 模拟器中的 Health 应用程序中读取数据

来自分类Dev

闪亮的应用程序仪表板中的页脚对齐

来自分类Dev

如何向Windows Phone 7应用程序添加事件提醒

来自分类Dev

Apache Cordova / Visual Studio 2015工具无法在IOS模拟器中启动应用程序

来自分类Dev

如何在Xcode(iOS)模拟器中手动安装应用程序

来自分类Dev

尝试在iOS模拟器中运行应用程序时收到奇怪的Xcode错误

来自分类Dev

iOS 7应用程序无法在Xcode 6模拟器中显示整个屏幕

来自分类Dev

重置模拟器后,iOS7中的应用程序崩溃

来自分类Dev

iOS模拟器无法安装应用程序?在Xcode 5中

来自分类Dev

将Adobe AIR应用程序部署到iOS模拟器中

来自分类Dev

将MAF应用程序部署到Jdeveloper中的iOS模拟器

Related 相关文章

  1. 1

    如何在Mac上的提醒中添加包含特定应用程序的提醒?

  2. 2

    iOS如何设置应用程序提醒

  3. 3

    用于模拟提议的示例Java Web应用程序(包括数据库脚本等)

  4. 4

    片段应用程序,如何添加页脚?

  5. 5

    类似于提醒应用程序iOS 7中的页面控制

  6. 6

    如何在 Windows 10 中模拟 iOS 应用程序

  7. 7

    无法在 iOS 模拟器中安装应用程序

  8. 8

    如何提醒用户iOS应用程序的新更新

  9. 9

    vuejs 应用程序中的粘性页脚

  10. 10

    如何从基于Azure的MVC应用程序中启用提醒?

  11. 11

    如何模拟iOS应用程序的更新

  12. 12

    我正在使用4.3版Android中的listview交叉应用程序的listview添加/删除页脚?

  13. 13

    为整个Android应用程序添加页脚

  14. 14

    让用户能够在 iOS 中添加应用程序后台功能

  15. 15

    为什么从iOS模拟器中打开我的iOS应用程序会失败/关闭?

  16. 16

    即将提交应用程序提醒

  17. 17

    在Android应用程序内提示或提醒

  18. 18

    提醒打开应用程序提供反馈

  19. 19

    如何从 iOS 11 模拟器中的 Health 应用程序中读取数据

  20. 20

    闪亮的应用程序仪表板中的页脚对齐

  21. 21

    如何向Windows Phone 7应用程序添加事件提醒

  22. 22

    Apache Cordova / Visual Studio 2015工具无法在IOS模拟器中启动应用程序

  23. 23

    如何在Xcode(iOS)模拟器中手动安装应用程序

  24. 24

    尝试在iOS模拟器中运行应用程序时收到奇怪的Xcode错误

  25. 25

    iOS 7应用程序无法在Xcode 6模拟器中显示整个屏幕

  26. 26

    重置模拟器后,iOS7中的应用程序崩溃

  27. 27

    iOS模拟器无法安装应用程序?在Xcode 5中

  28. 28

    将Adobe AIR应用程序部署到iOS模拟器中

  29. 29

    将MAF应用程序部署到Jdeveloper中的iOS模拟器

热门标签

归档