[iOS备忘录]UITextView多行输入,键盘隐藏问题[swift]
2015-08-21 09:47
555 查看
使用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)即可。
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)即可。
相关文章推荐
- mysql如何将多行数据合并成一行
- oracle实现多行合并的方法
- 顶部可滚动小广告位 的封装 (UIScrollView+PageControl)
- ios7 中关于导航切换影响UIScrollView的问题
- 两个UIScrollVIew能够同步滚动
- UIScrollview要加载大量数据的时候,考虑到内存的消耗问题,我们不可能全部加载完。
- UIScrollview 无缝循环滚动实现
- 解决 IOS的UIScrollView的自动布局
- Android自动隐藏键盘
- UIScrollView无法滚动的问题
- cocos2d-x 一些有用的函数
- IOS UIscrollView 控件
- 关于NGUI中UITexture贴图自定义的shader文件在UISrollView中不报错的解决方案
- 缩放图片
- UIScrollView缩放
- UIScrollView的属性总结
- UIScrollView常用属性及回调方法
- IOS-UIScrollView滚动图片的简单实现
- UIScrollView的使用大全--contentsize,contentInset和contentInset
- 使用UINavigationController后导致UIScollView尺寸变化(转)