ios - How to move view at the center of visible frame when keyboard appears? -
i have standard uiview
(containing image view, 2 textfield , button) embedded in root scroll view. i'm using autolayout put uiview
@ center of scroll view, shown in screenshot below.
i trying write method moves uiview
when keyboard appears, view appear @ center of smaller visible frame. achieve this, calculated heights.
- (void)keyboardwillshow:(nsnotification *)notification { // determines size of keyboard frame cgsize keyboardsize = [[[notification userinfo] objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; [uiview animatewithduration:0.3 animations:^{ // gets root 'uiview' frame , stores in variable cgrect viewframe = self.itemsview.frame; // height of visible frame once keyboard appears double visibleframeheight = (self.view.frame.size.height - keyboardsize.height); // how 'uiview' should move double offset = ((visibleframeheight - viewframe.size.height / 2); // moves 'uiview' viewframe.origin.y = -offset; self.itemsview.frame = viewframe; }]; }
the problem uiview
moved up, shown below.
why happening , how can fix it?
if view embedded in uiscrollview can adjust scrollview's contentoffset
move view (then animation free).
assuming scrollview available on outlet called scrollview
, , want keep view centered, shift content offset 1/2 keyboard height. - safer use uikeyboardframeenduserinfokey
final frame of keyboard:
- (void)keyboardwillshow:(nsnotification *)notification { // determines size of keyboard frame cgsize keyboardsize = [[[notification userinfo] objectforkey:uikeyboardframeenduserinfokey] cgrectvalue].size; self.scrollview.contentoffset = cgpointmake(0, keyboardsize.height / 2); } - (void)keyboardwillhide:(nsnotification *)notification { self.scrollview.contentoffset = cgpointzero; }
Comments
Post a Comment