【发布时间】:2017-01-08 18:15:33
【问题描述】:
我想控制打开的键盘并同时滚动表格视图。我现在必须关闭键盘才能滚动表格视图。
【问题讨论】:
-
您在表格视图单元格中使用文本字段吗?
标签: objective-c swift scroll keyboard
我想控制打开的键盘并同时滚动表格视图。我现在必须关闭键盘才能滚动表格视图。
【问题讨论】:
标签: objective-c swift scroll keyboard
这是一个基本的ViewController,用于处理在键盘显示期间需要显示视图的情况。你要做的就是继承BaseInputController和overrideshowAnimation和hideAnimation。展示您的新控制器进行测试!
class BaseInputController: UIViewController, BaseInputProtocol {
private var blur: UIVisualEffectView!
init(){
super.init(nibName: nil, bundle: nil)
modalTransitionStyle = .CrossDissolve
modalPresentationStyle = .OverCurrentContext
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
setUI()
setNotification()
}
private func setUI(){
blur = UIVisualEffectView(effect: UIBlurEffect(style: .Dark))
blur.alpha = 0
view.addSubview(blur)
let tap1 = UITapGestureRecognizer(target: self, action: #selector(BaseInputController.cancelHandle))
blur.addGestureRecognizer(tap1)
blur.snp_makeConstraints { (make) in
make.edges.equalTo(view)
}
}
@objc private func cancelHandle(){
dismissViewControllerAnimated(
false,
completion: nil
)
}
private func setNotification(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseInputController.keyBoardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseInputController.keyBoardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
}
func keyBoardWillShow(note:NSNotification){
guard let userInfo = note.userInfo,
height = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue().size.height,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber
else{ return }
UIView.animateWithDuration(
duration.doubleValue,
delay: 0,
options: UIViewAnimationOptions(rawValue: UInt(duration.integerValue) << 16),
animations: {
self.blur.alpha = 1
self.showAnimation(height)
},
completion: nil
)
}
func showAnimation(height: CGFloat){
}
func keyBoardWillHide(note:NSNotification){
guard let userInfo = note.userInfo,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber
else{ return }
UIView.animateWithDuration(
duration.doubleValue,
delay: 0,
options: UIViewAnimationOptions(rawValue: UInt(duration.integerValue) << 16),
animations: {
self.blur.alpha = 0
self.hideAnimation()
},
completion: {
if $0 { self.dismissViewControllerAnimated(true, completion: nil) }
}
)
}
func hideAnimation(){
}
func hideInput(){
}
}
【讨论】:
//根据键盘移动表格视图。
CGPoint pointInTable = [textView.superview convertPoint:textView.frame.origin toView:self.tblEditTask];
CGPoint contentOffset = self.tblEditTask.contentOffset;
contentOffset.y = (pointInTable.y - textView.inputAccessoryView.frame.size.height);
NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));
[self.tblEditTask setContentOffset:contentOffset animated:YES];
【讨论】:
您在 viewDidLoad 中注册通知并在您的 viewWillDisappear 中取消注册通知
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
只需通过键盘高度调整tableview的contentInset,然后将单元格滚动到底部即可:
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.myTableView.contentInset = contentInsets;
self.myTableView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
[UIView animateWithDuration:.3 animations:^(void)
{
self.myTableView.contentInset = UIEdgeInsetsZero;
self.myTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}];
}
【讨论】: