折腾:
[已解决]swift获取键盘显示之前的事件
之后,此处虽然可以通过:
textFieldDidBeginEditing
在
keyboardWillShow
之前传递view
但是:
之后出现的:
keyboardWillShow
被执行了3次
其中内部已经:
if keyboardHeight > bottomEmptyHeight { //calculate height to move up moveUpHeight = keyboardHeight – bottomEmptyHeight + bottomToKeyboardTopPadding gLog.verbose(“moveUpHeight=\(moveUpHeight)”) //123.0 self.view.frame.origin.y -= moveUpHeight } |
然后对应的,此处的继承了AutoMoveUpViewController的CustomerDetailViewController
的页面,还是没有向上移动。。。
ios change view.frame.origin.y frame not move
ios change frame origin but position not change
iphone – Change position of UIView doesn’t work – Stack Overflow
->好像是由于:CustomerDetailViewController页面中用的是自动布局?
-》但是之前的一个vc:
class LoginRegisterViewController: AutoMoveUpViewController, UITextFieldDelegate {
却是可以实现:
修改了frame的origin的y后,页面上移的啊
ios – Why a frame of a UIView is not updating in ViewDidLayoutSubviews? – Stack Overflow
最后很无奈,去通过:
更新tableview的约束条件去使得页面上移
然后滚动到对应的tableviewcell的:
var tableviewConstraintGroup:ConstraintGroup self.tableviewConstraintGroup = ConstraintGroup() self.view.addSubview(self.customerDetailTableview) self.tableviewConstraintGroup = constrain(self.customerDetailTableview){ customerDetailTableview in customerDetailTableview.left == customerDetailTableview.superview!.left customerDetailTableview.right == customerDetailTableview.superview!.right customerDetailTableview.top == customerDetailTableview.superview!.top customerDetailTableview.bottom == customerDetailTableview.superview!.bottom } // became first responder func textFieldDidBeginEditing(textField: UITextField) { gLog.debug(“textField=\(textField)”) findCellAndScroll(textField) } func findCellAndScroll(curSubview:UIView) { let keyboardHeight:CGFloat = 253.0 self.updateTableviewConstain(keyboardHeight) if let cellView = curSubview.superview?.superview where cellView is UITableViewCell { gLog.verbose(“cellView=\(cellView)”) //cellView=<Sales_App.LeftLabelRightTextfieldTableViewCell: 0x7c1ffe00; baseClass = UITableViewCell; frame = (0 794; 320 44); autoresize = W; layer = <CALayer: 0x7ce43e70>> //cellView=<Sales_App.TextviewTableViewCell: 0x7aa3cc00; baseClass = UITableViewCell; frame = (0 402; 320 44); autoresize = W; layer = <CALayer: 0x79ed20d0>> if let curIndexPath = self.customerDetailTableview.indexPathForCell(cellView as! UITableViewCell) { gLog.verbose(“curIndexPath=\(curIndexPath)”) //curIndexPath=<NSIndexPath: 0x7af4ada0> {length = 2, path = 4 – 6} //curIndexPath=<NSIndexPath: 0x79ee8a10> {length = 2, path = 2 – 0} dispatchMain_sync(0.1, thingsTodo: { self.customerDetailTableview.scrollToRowAtIndexPath(curIndexPath, atScrollPosition: UITableViewScrollPosition.Middle, animated: false) }) } } } func updateTableviewConstain(moveupHeight:CGFloat = 0) { gLog.verbose(“moveupHeight=\(moveupHeight)”) self.tableviewConstraintGroup = constrain(self.customerDetailTableview, replace: self.tableviewConstraintGroup) { customerDetailTableview in customerDetailTableview.left == customerDetailTableview.superview!.left customerDetailTableview.right == customerDetailTableview.superview!.right customerDetailTableview.top == customerDetailTableview.superview!.top customerDetailTableview.bottom == customerDetailTableview.superview!.bottom – moveupHeight } self.customerDetailTableview.updateConstraints() // self.customerDetailTableview.layoutSubviews() } |
即可实现:
当点击输入框:
后,页面滚动到对应的输入框:
转载请注明:在路上 » [已解决]keyboardWillShow中改变了self.view.frame.origin.y但是页面并没有先上移动