[iOS备忘录]UITextView多行输入,键盘隐藏问题[swift]
来源:互联网 发布:ubuntu ftp服务器搭建 编辑:程序博客网 时间:2024/06/10 17:44
使用UITextView进行多行输入,输入完毕后关闭键盘
UIScrollView与UITextView配合使用,可实现多行输入。
var msgText: UITextView!
let sc = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "handleKeyboardWillShowNotification:", name:
UIKeyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: "handleKeyboardWillHideNotification:", name:
UIKeyboardWillHideNotification, object: nil)
// scroll
sc.frame = self.view.frame
sc.delegate = self
sc.contentSize = CGSize(width: 250,height: 150)
self.view.addSubview(sc);
msgText = UITextView(frame: CGRectMake(10, screenHeight-100, width-20, 40))
msgText.editable = true
msgText.layer.masksToBounds = true
msgText.layer.cornerRadius = 10.0
msgText.layer.borderWidth = 1
msgText.layer.borderColor = UIColor.lightGrayColor().CGColor
msgText.font = UIFont.systemFontOfSize(CGFloat(20))
self.view.addSubview(msgText)
// 关闭键盘的按钮
let accessoryView: UIView = UIView(frame: CGRectMake(0, 0, 210, 40))
accessoryView.backgroundColor = UIColor.clearColor()
let closeBtn = UIButton.buttonWithType(UIButtonType.System) as! UIButton
closeBtn.frame = CGRectMake(210, 10, width-100, 30)
closeBtn.setTitle("閉じる", forState: .Normal)
closeBtn.addTarget(self, action: "closeBtnClicked", forControlEvents: UIControlEvents.TouchUpInside)
accessoryView.addSubview(closeBtn)
msgText.inputAccessoryView = accessoryView
sc.addSubview(msgText)
}
func handleKeyboardWillShowNotification(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size
var txtLimit = msgText.frame.origin.y + msgText.frame.height + 8.0
//var btnLimit = sendBtn.frame.origin.y + sendBtn.frame.height
let kbdLimit = myBoundSize.height - keyboardScreenEndFrame.size.height
if txtLimit >= kbdLimit {
//if btnLimit >= kbdLimit {
sc.contentOffset.y = btnLimit - kbdLimit
}
}
func handleKeyboardWillHideNotification(notification: NSNotification) {
sc.contentOffset.y = 0
}
如果设置了按钮需要按钮与输入栏一起联动的话,需要将按钮也添加到scroll里,并修改limit(btnLimit)即可。
- [iOS备忘录]UITextView多行输入,键盘隐藏问题[swift]
- ios UITextField和 UITextView输入完毕后隐藏键盘
- IOS UITextView实现自动隐藏键盘
- iOS-UITextField和UITextView隐藏键盘
- iOS-UITextField和UITextView隐藏键盘
- IOS UItextfield 和 UItextView 键盘隐藏
- 使用UITouch隐藏UITextField/UITextView输入键盘的方法-iOS开发
- UITextView, UITextField输入内容, 键盘挡住问题
- UITextField和 UITextView输入完毕后隐藏键盘
- UITextField和 UITextView输入完毕后隐藏键盘
- UITextView及键盘隐藏
- UITextView及键盘隐藏
- UITextView及键盘隐藏
- iOS UITextField,UITextView键盘遮挡问题
- ios swift uitextview如何应对键盘的遮挡
- Swift 多文本输入框UITextView
- iOS菜鸟学习——UITextView return键隐藏键盘
- UITextView 委托及键盘隐藏
- HDU 4746 Mophues (莫比乌斯反演应用)
- iOS中使用storyboard实现页面跳转,Segue详解及简单的数据传递
- UVA 10815 Andy's First Dictionary
- 设置元素的属性attr 和setAttribute
- 开源平台选型建议
- [iOS备忘录]UITextView多行输入,键盘隐藏问题[swift]
- Linux命令(1)
- linux c语言将系统调用的执行结果赋给变量
- java线程的同步方法
- 最少钱币数 (2013中南大学年研究生上机复试题[暴力])
- 黑马程序员-java基础-IO流的简单介绍
- POJ 2299 Ultra-QuickSort 归并排序
- 磁盘阵列RAID理解
- php内核探索系类文章