ios - Why the two CGRect don't intersect when they do visually? -
i building app. needs accept user input uitextfields. , keyboard hide text field need move view when keyboard cgrect intersects text field's frame.
i followed this tutorial added of own logic because have multiple text fields.
here relevant code: (the whole thing in vc conforms uitextfielddelegate)
var focusedtextfield: uitextfield? var viewmovedup = false var keyboardsize: cgrect! override func viewdidappear(animated: bool) { super.viewdidappear(animated) nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("keyboardwillshow:"), name:uikeyboardwillshownotification, object: nil); nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("keyboardwillhide:"), name:uikeyboardwillhidenotification, object: nil); nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("onrotate:"), name:uideviceorientationdidchangenotification, object: nil); } override func viewdiddisappear(animated: bool) { super.viewwilldisappear(animated) nsnotificationcenter.defaultcenter().removeobserver(self) } func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() return true } func textfielddidbeginediting(textfield: uitextfield) { focusedtextfield = textfield } func onrotate (notification: nsnotification) { view.endediting(true) } func keyboardwillshow(notification: nsnotification) { if let userinfo = notification.userinfo { if let keyboardsize = (userinfo[uikeyboardframeenduserinfokey] as? nsvalue)?.cgrectvalue() { self.keyboardsize = keyboardsize assert(focusedtextfield != nil) if cgrectintersectsrect(focusedtextfield!.bounds, keyboardsize){ moveview(up: true) } } } } func keyboardwillhide(notification: nsnotification) { if viewmovedup { moveview(up: false) } } func moveview (up up: bool) { let keyboardheight = keyboardsize.height let movement = (up ? -keyboardheight : keyboardheight) uiview.animatewithduration(0.3, animations: { self.view.frame = cgrectoffset(self.view.frame, 0, movement) }) viewmovedup = } if don't want read whole code, i'll explain gist of it. when user taps on 1 of text fields, textfielddidbeginediting gets called. sets focusedtextfield text field user editing. keyboardwillshow gets called. gets keyboard size , assign class-level variable called keyboardsize checks if focused text field (remember that?) covered keyboard (via cgrectintersectrect). if move view calling moveview. method works fine no need explain.
now on problem!
let's @ screen shot of vc:
when tap on "enter a" text field, view moves expected. when tap on "enter p" text field, keyboard shows , covers text field completely.
after debugging, found that
cgrectintersectsrect(focusedtextfield!.bounds, keyboardsize)
returns false moveview not called. "enter p" text field , keyboard size follows:
bounds of text field: x: 62 y: 94.5 height: 32.5 width: 278 keyboard size: x: 0 y: 158 height: 162 width: 568 just these figures, don't think overlap. visually, do!
i tried change focusedtextfield!.bounds focusedtextfield.frame still doesn't work.
why happening? how fix it?
the problem in code:
cgrectintersectsrect(focusedtextfield!.bounds, keyboardsize) ...you comparing apples , oranges:
focusedtextfield!.boundsin coordinate space offocusedtextfieldkeyboardsizein coordinate space of the window
(and reason why focusedtextfield.frame didn't work is in yet another coordinate space, of text field's superview.)
those 2 different coordinate spaces, cannot compare these rects. have convert 1 of them coordinate space of other.
for example, think it:
newkeyboardsize = focusedtextfield.convertrect(keyboardsize, fromview:nil) now newkeyboardsize , focusedtextfield.bounds should in same coordinate space.


Comments
Post a Comment