swift - Get keyboard size on iOS 8 not working with external keyboard -
been looking @ several post now, none seem me. want uitableview
s bottom constraint set keyboard never on top of tableview. seems impossible in ios 8. work when using normal soft keyboard, when removing in simulator or using real hardware keyboard, still thinks there soft keyboard.
the code:
var currentkeyboardheight:cgfloat = 0.0 override public func viewdidload() { super.viewdidload() nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillshow:", name: uikeyboardwillshownotification, object: nil) nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillhide:", name: uikeyboardwillhidenotification, object: nil) } deinit { nsnotificationcenter.defaultcenter().removeobserver(self) } func keyboardwillshow(notification: nsnotification) { if let userinfo = notification.userinfo { var keyboardrect = userinfo[uikeyboardframeenduserinfokey]!.cgrectvalue() let windowrect = view.window!.convertrect(keyboardrect, fromwindow:nil) let viewrect = view.convertrect(windowrect, fromview:nil) let deltaheight = viewrect.size.height - currentkeyboardheight currentkeyboardheight = viewrect.size.height tableviewbottomconstraint.constant = currentkeyboardheight uiview.animatewithduration(0.25, animations: { () -> void in self.view.layoutifneeded() }) println("show keyboard: \(currentkeyboardheight)") } } func keyboardwillhide(notification: nsnotification) { if let userinfo = notification.userinfo { var keyboardrect = userinfo[uikeyboardframeenduserinfokey]!.cgrectvalue() keyboardrect = self.view.convertrect(keyboardrect, fromview: nil) let deltaheight = keyboardrect.size.height - currentkeyboardheight currentkeyboardheight = 0 tableviewbottomconstraint.constant = currentkeyboardheight uiview.animatewithduration(0.25, animations: { () -> void in self.view.layoutifneeded() }) println("hidekeyboard \(currentkeyboardheight)") } }
when selecting uitextview
in last tableviewrow printed:
show keyboard: 260.0
which wrong. if turn on soft keyboard (cmd+shift+k):
show keyboard: 260.0 show keyboard: 297.0 show keyboard: 297.0
which correct. , turn off (cmd+shift+k):
hidekeyboard 0.0 show keyboard: 260.0 hidekeyboard 0.0
which correct
update 1:
according comment on answer (https://stackoverflow.com/a/2893327/511299) uikeyboardwillshownotification
fire when using accessoryview
am.
func setupkeyboardbuttons() { let numbertoolbar = uitoolbar(frame: cgrectmake(0,0,320,50)) numbertoolbar.barstyle = uibarstyle.default let button1 = uibarbuttonitem(title: cancel_text, style: uibarbuttonitemstyle.plain, target: self, action: "keyboardcancelbuttontapped:") let button2 = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.flexiblespace, target: nil, action: nil) let button3 = uibarbuttonitem(title: save_text, style: uibarbuttonitemstyle.plain, target: self, action: "keyboarddonebuttontapped:") button1.tintcolor = uicolor.darkgraycolor() numbertoolbar.items = [button1, button2, button3] numbertoolbar.sizetofit() commenttextview.inputaccessoryview = numbertoolbar }
you can detect if external keyboard connected , and set keyboard's height 0 doing that.
objective-c version:
cgrect finalkeyboardframe = [[info objectforkey:uikeyboardframeenduserinfokey] cgrectvalue]; finalkeyboardframe = [self convertrect:finalkeyboardframe fromview:self.window]; nslog(@"origin.y: %f", finalkeyboardframe.origin.y);
shows y coordinate depending if external keyboard connected or not:
origin.y: 258.000000 (not connected)
origin.y: 474.000000 (connected)
Comments
Post a Comment